Here we’re going to cover how to find out what is using disk space in Linux with the ‘du’ disk usage command.
This command is extremely useful for tracking down large files, allowing you to find large files and free up precious disk space.
As a standard GNU command it’s useful to know how to use as there’s a good chance it’s already installed and ready to use, you don’t have to rely on installing any other packages. In CentOS it’s part of the coreutils package which provides a number of basic GNU tools and scripts by default.
How To Use du – Command Examples
By default if we run the ‘du’ command by itself, it will recursively list the space that each directory in the current working directory is using in kb.
For example if I run ‘du’ in the ‘/’ directory, we can see that we get over 17,000 results on my minimal CentOS 7 Linux installation.
[[email protected] /]# pwd / [[email protected] /]# du | wc -l 17253
While this full output in itself is generally not very useful, we can use additional options to make the output a lot more useful.
Now, onto our 13 most useful ‘du’ command examples!
1. Specify Maximum Depth
As in the above example, du recursively queries all directories below where it has been executed. We can make this more useful by specifying how deep we should check with --max-depth, which can be shortened to the -d option. By setting this to 1, we will look 1 directory deep and display the size in use of all directories within the current location. If we then specify a depth of 2 we will then see the disk usage of all directories at the next level, and so on.
[[email protected] test]# du -d 1 5520 ./test1 22080 ./test2 33120 . [[email protected] test]# du -d 2 2760 ./test1/test1-sub 5520 ./test1 16560 ./test2/test2-sub 22080 ./test2 33120 .
As shown with a maximum depth of 1 specified we can see the total disk space in kb of directories in the current directory. Once we specify a depth of 2 we go another level deeper and now see the disk space used by the sub directories.
2. Check Specific Directory
By default the ‘du’ command will run in the context of the current working directory. Rather than changing into the directory that you want to investigate, we can simply specify the directory as an argument to the command.
[[email protected] /]# pwd / [[email protected] /]# du /root/test/test1/ 2760 /root/test/test1/test1-sub 5520 /root/test/test1/
This shows that while our working directory is ‘/’ we can get the disk space used from a specified directory.
3. Human Readable Output
I’m not sure about you, but reading large strings of kb isn’t a very easy way for me to quickly get an accurate idea of how much space is in use. This is where the -h option comes in, which will output human readable format by converting to KB, MB, or GB for example.
Here’s what the contents of ‘/’ look like with the -h option, as you can see this makes more sense to a person viewing the output than a longer string of numbers.
[[email protected] /]# du -h -d 1 / 118M /boot 0 /dev 0 /proc 8.6M /run 0 /sys 21M /etc 36M /root 140K /tmp 116M /var 860M /usr 0 /home 0 /media 0 /mnt 0 /opt 0 /srv 1.2G /
4. Display Disk Usage In Bytes
We can also go the other way and display the numbers in bytes rather than the default kb values with the -b option, which may be more useful for monitoring scripts that need specific values.
[[email protected] test]# du -b 2823810 ./test1/test1-sub 5647636 ./test1 16942830 ./test2/test2-sub 22590460 ./test2 96009266 .
5. Show Disk Usage For Files
By default so far we have seen the disk space usage of directories rather than specific files. While this does allow us to check the directories to see what is using the space, we can optionally make use of the -a option which will show all files as well.
[[email protected] test]# du -h -d 2 -a 2.7M ./test1/test1-sub 2.7M ./test1/1 5.4M ./test1 17M ./test2/test2-sub 2.7M ./test2/7 2.7M ./test2/8 22M ./test2 2.7M ./1 2.7M ./2 33M .
Now we can see the space used within the directories as well as the specific files within.
6. Report Total Disk Space
With the -c option we can have the total space reported at the bottom of the output, though usually this isn’t that helpful as we also see this number listed for the ‘.’ directory, that is the directory du is being run in.
[[email protected] test]# du -h -d 2 -c 2.7M ./test1/test1-sub 5.4M ./test1 17M ./test2/test2-sub 22M ./test2 33M . 33M total
We’re likely better off using the -s option which only outputs the summary if that’s all you’re after.
[[email protected] test]# du -h -s 33M .
7. Sort Output By Disk Usage Size
While not built into the du command, we can pipe it to the sort command in order to list files in order of file size, such as smallest to largest. This is extremely useful as we can search the whole disk and order the output based on file size, allowing us to quickly locate large files.
[[email protected] test]# du -h -a / | sort -h | tail -n 15 105M /usr/lib/firmware 113M /usr/lib/modules/3.10.0-327.22.2.el7.x86_64/kernel 116M /usr/lib/modules 116M /usr/lib/modules/3.10.0-327.22.2.el7.x86_64 118M /boot 128M /usr/lib64 169M /var/cache/yum 169M /var/cache/yum/x86_64 169M /var/cache/yum/x86_64/7 170M /var/cache 209M /var 265M /usr/share 361M /usr/lib 860M /usr 1.3G
Here we make use of the -h option in sort, which sorts based on human readable numbers.
8. Show Last Modification Time
While searching for files using disk space to potentially be removed it can be beneficial to view the last modified time stamp. For example if used disk space is increasing over time then the most recently modified files are a good place to investigate, for instance we may have a large log file that is constantly being modified and written to.
[[email protected] log]# du -h -d 1 --time -a 0 2016-08-29 14:18 ./tallylog 24K 2016-08-30 09:22 ./lastlog 4.0K 2016-08-30 09:22 ./btmp 8.0K 2016-08-30 09:22 ./wtmp 0 2014-06-10 16:27 ./ppp 332K 2016-08-30 10:01 ./messages 8.0K 2016-08-30 09:22 ./secure 4.0K 2016-08-30 09:20 ./maillog 0 2016-08-29 14:18 ./spooler 4.0K 2016-08-30 09:20 ./tuned 308K 2016-08-30 10:01 ./audit 1.7M 2016-08-29 14:21 ./anaconda 4.0K 2016-08-30 10:01 ./cron 4.0K 2016-08-30 09:20 ./wpa_supplicant.log 8.0K 2016-08-29 16:23 ./yum.log 0 2016-07-19 01:30 ./httpd 12K 2016-08-30 09:20 ./boot.log 124K 2016-08-29 14:23 ./dmesg.old 124K 2016-08-30 09:20 ./dmesg 2.7M 2016-08-30 10:01 .
While the last modified time is default, we can optionally specify access time and change time with ‘--time=atime’ or ‘--time=ctime’ respectively.
9. Only Show Larger Than Specified Size
Rather than showing the size of all files and directories, we can set a threshold to only show results larger than a specified number. This may be helpful if we want to search for files larger than a specified size which would be useful in finding large single files that may be using the majority of your disk space.
[[email protected] test]# du -h -t 100M -a / 118M /boot 116M /var 105M /usr/lib/firmware 102M /usr/lib/locale/locale-archive 102M /usr/lib/locale 113M /usr/lib/modules/3.10.0-327.22.2.el7.x86_64/kernel 116M /usr/lib/modules/3.10.0-327.22.2.el7.x86_64 116M /usr/lib/modules 361M /usr/lib 128M /usr/lib64 265M /usr/share 860M /usr 1.2G /
In this example only files and directories that are larger than 100MB are returned.
10. Exclude Pattern From Search
With the --exclude option we can optionally not show anything that matches a specified pattern in the search results.
[[email protected] test]# du -h -d 1 -a 5.4M ./test1 22M ./test2 63M ./3.iso 2.7M ./1.txt 2.7M ./2.mp3 96M . [[email protected] test]# du -h -d 1 -a --exclude="*.iso" 5.4M ./test1 22M ./test2 2.7M ./1.txt 2.7M ./2.mp3 33M .
In this example we can see that we have a .iso file in our output at first, however after excluding this specific string it no longer displays in the output. This is useful if you’re aware of certain files that you want to exclude from the results as you may not want to investigate some set of known files.
11. List Inode Usage
By default du displays block usage, however we can instead change this to show the inode usage with the –inodes option.
[[email protected] test]# du -d 2 --inodes 2 ./test1/test1-sub 4 ./test1 7 ./test2/test2-sub 10 ./test2 18 .
12. Remove New Line Characters
By default the output of the du command shows one result per line, which helps a lot when we go to read the output, however if you are perhaps creating a script to collect the data and perform a task on the output it may be easier to do so with a string of data without the newline character. This is where the -0 option comes in, it replaces the new line characters with a null byte and outputs everything on a single line.
[[email protected] test]# du -0 2760 ./test1/test1-sub5520 ./test116560 ./test2/test2-sub22080 ./test293788 .[[email protected] test]#
13. Don’t Display Content From Different File Systems
By default the du command will search recursively either from where specified or otherwise from within the current working directory. If we have other files systems mounted, such as an NFS share mounted to /mnt, we may not want to go and look through these other file systems which may be from different disks or partitions. This is where the -x option comes in, it will ignore all other file systems that it may find.
[[email protected] test]# du -d 1 -x / 21400 /etc 97072 /root 140 /tmp 213716 /var 880124 /usr 0 /home 0 /media 0 /mnt 0 /opt 0 /srv 1212456 /
In this example note that we are running du in ‘/’ however many directories such as /boot are missing. This is because they are running on different file systems, while ‘/’ is XFS on /dev/sda2, /boot is on the /dev/sda1 disk and runs a different file system.
Here we have shown the power of the ‘du’ command throughout practical guided examples. This tutorial should help you in tracking down disk space usage on your UNIX or Linux system.
While there are a lot of other disk space utilities out there, ‘du’ is almost always available by default, making it a good solid choice for searching and finding which files are using your disk space.