To check and view and total disk size used by files in each and every directories and sub-directories in Linux, we can make use of du command. du command, is used to estimate file space usage, and is useful is you intend to list the disk usage by directory.
To list the summary of total file size and disk usage by the directory and every sub-directories recursively, use the one of the following commands:
du -h
du --human-readable
The -h or –human-readable option instructs du to print sizes in human readable format in kilobytes (KB), megabytes (MB) or gigabytes (GB).
Output generated by above command can be very long, as it goes through each and every sub-directories recursively. The following commands will display only the total file disk usage size for sub-directories contained within a directory, including the sum of all nested sub-directories but without printing those nested sub-folders.
du -h -d1
du --summary --max-depth=1
-dN or –max-depth=N option print the total for a directory (or file, with –all) only if it is N or fewer levels below the command line argument.
Use one of the following commands to display only the total file disk usage size for files and sub-directories contained within a directory:
du -sh *
du --summary --human-readable *
du -h -d0 *
du --human-readable --max-depth=0 *
The “*” supplies the list of files and sub-folders within the current folder to the du command. You can change it to the full path of the directory you want to know its content disk usage. The additional -s or –summary option instructs du to display only a total for each argument.
If you just want to du command to compute the total disk space used by files inside a folder, but not the sum of total that include its subfolders, use the following command:
du -Sh
du --separate-dirs --human-readable
The -S or –separate-dirs option tells du not include size of subdirectories.
To sort the files and folders in descending order from largest total disk usage size at the top to smallest at the bottom:
| sort -hr
To sort the files and directories in ascending order – from smallest to biggest:
| sort -h
Note that if you’re not using -h option for the du command, you can replace that “-h” (which compares human readable numbers) to “-n” (which compares according to string numerical value) for sort command.