10 Useful du (Disk Usage) Commands to Find Disk Usage of Files and Directories

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
du /home/inimist
du /home/inimist
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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
du -h /home/inimist
du -h /home/inimist
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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
du -sh /home/inimist
du -sh /home/inimist
du -sh /home/inimist

4. Using -a flag with du command displays the disk usage of all the files and directories.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
du -a /home/inimist
du -a /home/inimist
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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
du -ah /home/home
du -ah /home/home
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).

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
du -k /home/inimist
du -k /home/inimist
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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
du -mh /home/inimist
du -mh /home/inimist
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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
540M /home/inimist
540M /home/inimist
540M /home/inimist
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
540M total
540M total
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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
du -ah --exclude="*.txt" /home/inimist
du -ah --exclude="*.txt" /home/inimist
du -ah --exclude="*.txt" /home/inimist

10. Display the disk usage based on modification of time, use the flag –time as shown below.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
du -ha --time /home/inimist
du -ha --time /home/inimist
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
du -hs /home/ | sort -rh | head -10
du -hs /home/ | sort -rh | head -10
du -hs /home/ | sort -rh | head -10
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo du -ma /home/ | sort -n -r | head -n 20
sudo du -ma /home/ | sort -n -r | head -n 20
sudo du -ma /home/ | sort -n -r | head -n 20
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
find /home -type f -printf '%s %p\n' | sort -nr | head -20
find /home -type f -printf '%s %p\n' | sort -nr | head -20
find /home -type f -printf '%s %p\n' | sort -nr | head -20
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo du -a /home/ | sort -n -r | head -n 20
sudo du -a /home/ | sort -n -r | head -n 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

Leave a Reply