With the df command, it is easy to see when a disk is running out of space. The next problem for the system administrator is to know what to do when that happens.
Another useful command to help you out is the du command. The du command shows the disk usage for a specific directory (by default, the current directory). This is a quick way to determine if you have any obvious disk hogs on the system.
By default, the du command displays all the files, directories, and subdirectories under the current directory, and it shows how many disk blocks each file or directory takes. For a standard-sized directory, this can be quite a listing. Here’s a partial listing of using the du command:
$ du 484 ./.gstreamer-0.10 8 ./Templates 8 ./Download 8 ./.ccache/7/0 24 ./.ccache/7 368 ./.ccache/a/
The number at the left of each line is the number of disk blocks that each file or directory takes. Notice that the listing starts at the bottom of a directory and works its way up through the files and subdirectories contained within the directory.
If you encounter below error while running the du command:
du: command not found
you may try installing the below package as per your choice of distribution:
Distribution | Command |
---|---|
OS X | brew install coreutils |
Debian | apt-get install coreutils |
Ubuntu | apt-get install coreutils |
Alpine | apk add coreutils |
Arch Linux | pacman -S coreutils |
Kali Linux | apt-get install coreutils |
CentOS | yum install coreutils |
Fedora | dnf install coreutils |
Raspbian | apt-get install coreutils |
du Command Examples
1. To get space usage for all:
# du -a
2. To print the aparent size instead of disk usage:
# du --apparent-size
3. To report the usage in spacified block size:
# du -B=SIZZE # du --block-size=1024
4. To print the usage in aparent size of 1:
# du -b # du --bytes
5. To produce a grand total:
# du -c # du --total
6. To dereference only symlinks that are listed on the command line:
# du -D # du --dereference-args
7. To print sizes in human readable format:
# du -h
8. To print sizes in human readable format with powes of 1000 not 1024:
# du --si
9. To print the usage with block size as 1024 i.e. 1k:
# du -k
10. To count sizes many times if hard linked:
# du -l # du --count-links
11. To print the usage with block size as 1024*1024=1048576 i.e. 1M:
# du -m
12. To dereference all symbolic links:
# du -L # du --dereference
13. To not to follow any symbolic links (this is the default):
# du -P # du --no-dereference
14. To end each output line with 0 byte rather than newline:
# du -0 # du --null
15. To do not include size of subdirectories:
# du -S # du --separate-dirs
16. To display only a total for each argument:
# du -x # du --one-file-system
17. To exclude files that match any pattern in FILE:
# du -X # du --exclude-from=FILE
18. To exclude files that match PATTERN:
# du --exclude=PATTERN
19. To print the total for a directory:
# du --max-depth=N
20. To show time of the last modification of any file in the directory:
# du --time
21. To show time as WORD instead of modification time: atime, access, use, ctime or status:
# du --time=WORD
22. To show times using style STYLE:
# du --time-style=STYLE
23. To get the help:
# du --help
24. To get the version:
# du --version
Final Thoughts
The du command by itself can be somewhat useless. It’s nice to be able to see how much disk space each individual file and directory takes up, but it can be meaningless when you have to wade through pages and pages of information before you find what you’re looking for.
You can use the following command-line parameters with the du command to make things a little more legible:
- -c: Produce a grand total of all the files listed.
- -h: Print sizes in human-readable form, using K for kilobyte, M for megabyte, and G for gigabyte.
- -s: Summarize each argument.