The Linux du
(abbreviation of “Disk Usage”) is a standard Unix/Linux command which is used to check the disk usage of files and directories. The du command has many parameters that can be used to get the results in different choices.
The du command accepts different options which help to display results by date and time, highest or latest files and folders etc.
1. To find the disk usage summary of a /home/inimist folder tree its sub folders. Enter the command as:
du /home/inimist
2. Using -h
option with du
command shows results in “Human Readable Format“. it means you can see sizes in Bytes, Kilobytes, Megabytes, Gigabytes etc.
du -h /home/inimist
3. To get the summary of a grand total disk usage size of an directory use the option -s
as follows.
du -sh /home/inimist
4. Using -a
flag with du
command displays the disk usage of all the files and directories.
du -a /home/inimist
5. Using -a
flag along with -h
displays disk usage of all files and folders in human readeable format. The below output is more easy to understand as it shows the files in Kilobytes, Megabytes etc.
du -ah /home/home
6. The -k
option is used to find out the aggregated disk usage of a directory and its subdirectories in Kilobytes. (1 Kilobyte = 1024 bytes).
du -k /home/inimist
7. Similarly -m
flag counts the blocks size in MB units. -h
option can be used to read size in Human readable format. For example, -mh
or -kh
as shown in previous example as well.
du -mh /home/inimist
8. The -c
flag aggregate the disk space size and puts grand total as the last line. If a directory took 540M space, then the last last two line of the output would be.
540M /home/inimist
540M total
9. The below command calculates and displays the disk usage of all files and directories, but excludes the files that matches given pattern. The below command excludes the “.txt” files while calculating the total size of diretory. So, this way you can exclude any file formats by using flag -–exclude
. See the output there is no txt files entry.
du -ah --exclude="*.txt" /home/inimist
10. Display the disk usage based on modification of time, use the flag –time
as shown below.
du -ha --time /home/inimist
Finding Largest files in a file system
Using sort and head option results are sorted recursively. By using head option along with n parameter number of results can be limited to a given number. This combination of commands can be very useful to find the largest files taking up space on your server.
Show largest file and directories sizes in linux
du -hs /home/ | sort -rh | head -10
sudo du -ma /home/ | sort -n -r | head -n 20
find /home -type f -printf '%s %p\n' | sort -nr | head -20
sudo du -a /home/ | sort -n -r | head -n 20
See how to find out largest files for more details on finding largest files on a linux machine.
One thought on “10 Useful du (Disk Usage) Commands to Find Disk Usage of Files and Directories”