Bash list size of file




















The first method is to use the good old ls command. When paired with other commands, the ls command can grab the size of a file. To implement the script, we need the full path of the file, list the file information with ls, and grab the size using a command such as awk. The above script is relatively simple. Using the echo and read command, we get the name of the full path of the file. Next, we use the ls -lh command to list all directories and the size in human-readable format, and finally, pass the output to awk and grab the size as the 5 th variable.

Another method we can use to grab the size of a file in a bash script is the wc command. The wc command returns the number of words, size, and the size of a file in bytes. As you know, reading the file size in bytes is not very easy. You can also combine this with other options, such as the -h human-readable option:. It could also be due to the differences between real file sizes and the disk space required to store them. If you need to match the output of other commands, experiment with the --block option in du.

Use Google Fonts in Word. Use FaceTime on Android Signal vs. Customize the Taskbar in Windows What Is svchost. Best Smartwatches. Best Gaming Laptops. Best Smart Displays.

Best Home Security Systems. Best External Solid State Drives. Best Portable Chargers. Best Phone Chargers. Best Wi-Fi Range Extenders. Best Oculus Quest 2 Accessories. Best iPad Air Cases. Awesome PC Accessories. Best Linux Laptops. Best Wireless iPhone Earbuds. Best Bluetooth Trackers. Best eReaders. Best VPN. Browse All News Articles. Windows 11 Uninstall Clock. The problem with this approach is that all the files with size less than 1 MB will also be displayed with file size 1 MB.

The ls command also has -s option to display size. You should combine with -h to show the file size in human readable form. You can also use the stat command in Linux to check the file size. Please enter at least 3 characters 0 results found. Abhishek Prakash. Quick tip to display file size in Linux using the ls command. Table of Contents. The du command in Linux is used for checking the size of directory. Note: In Linux, a directory is the equivalent of a folder in Windows.

A directory may have directories inside called subdirectories , or it may only contain files. The du command stands for disk usage. This command is included by default in most Linux distributions. You can display the size of your current directory by typing du in the command line:. The system should display a list of the contents of your home directory, with a number to the left.

That number is the size of the object in kilobytes. Each entry will start with a number and a letter. The number is the amount of space used, and the letter usually K, M, or G indicates Kilobytes, Megabytes, or Gigabytes. For example:. To find the size of a specific directory different from your current working directory. The du command allows you to specify a directory to examine:. You may see some entries with an error, as in the image below.

This happens when your user account does not have permission to access a particular directory.



0コメント

  • 1000 / 1000