In this post, we will be discuss the command “du” or disk usage. The du command is a handy command to check folder/file sizes in Linux. This command comes with a lots of options which when combining them together can give you a very powerful way to check file/folder size usage of your disk.
Syntax for Linux du command
du [OPTION]... [FILE]...
Basic du command usage examples
Example 1: Execute du command with no options. As you can see in this example, using du without any option will list folders recursively by default the size of each file under the directory you chose. The file size here is shown in KB by default. It will show the hidden files and the size of the parent directory as an entry, and the child directories as separate entries. However it will go up to the last directory
[root@linuxnix /]# du /opt 4 /opt/rh 8 /opt
Example 2: If you don’t want to see output in KB’s, use the -h option for a human readable output. Adding the -h option will give you the output in a more readable file size format, with base 1024, it will show the 1024 KB file as 1M which is more easier to read.
[root@linuxnix mnt]# du -h /mnt 100K /mnt/dir1 104K /mnt
Example 3: If you are wishing to get the disk space utilization of the whole directory, without going into details of any sub directories. Use the -s option for a summarized output and eventually files, you can use the -s option.
[root@linuxnix ~]# du -sh /var 1.1G /var
Or even one level down than just /var to include sub-directories
[root@linuxnix ~]# du -sh /var/* 4.0K /var/account 151M /var/cache 4.0K /var/crash 4.0K /var/cvs 4.0K /var/nis 4.0K /var/opt 4.0K /var/preserve 384K /var/run 73M /var/spool 160K /var/tmp 699M /var/www 12K /var/yp
Example 4: Using the -a option “with caution”. If you want to go as much granular as possible, up to the last file in the chain, including any hidden files, use the -a option. Please note that you can not use -s and -a in the same command.
[root@linuxnix mnt]# du -ah /mnt 0 /mnt/dir1/.file5 96K /mnt/dir1/file1 0 /mnt/dir1/file3 0 /mnt/dir1/.file6 0 /mnt/dir1/file2 100K /mnt/dir1 104K /mnt
Example 5: We can even control how many levels we can go down the sub directories using the[ –max-depth=N] option. It basically will view everything as long as it is N or less levels under the directory you specified.
[root@linuxnix mnt]# du -ah --max-depth=1 /var/ 4.0K /var/local 12K /var/yp 4.0K /var/crash 160K /var/tmp 4.0K /var/opt 384K /var/run 24K /var/lock 0 /var/mail 4.0K /var/account 145M /var/lib 699M /var/www
Example 6: Now let us do the same as the previous example, but add the –time option
this will increase your visibility, it will show you a new column with the time last modified shown.
[root@linuxnix mnt]# du -ah --max-depth=1 --time /var/ 4.0K 2011-09-23 13:50 /var/local 12K 2015-08-07 18:38 /var/yp 4.0K 2016-01-06 13:36 /var/crash 160K 2016-02-08 11:49 /var/tmp 4.0K 2011-09-23 13:50 /var/opt 384K 2016-03-03 17:46 /var/run 24K 2016-03-03 03:50 /var/lock 0 2015-08-07 18:35 /var/mail 4.0K 2015-08-07 18:46 /var/account 145M 2016-03-03 19:09 /var/lib 699M 2016-02-28 09:10 /var/www 4.0K 2011-09-23 13:50 /var/nis 49M 2016-03-03 19:14 /var/log 1.1G 2016-03-03 19:14 /var/
Example 7: What if you are not interested in some file patterns, and you wish to exclude them, use the [–exclude=pattern] option
Let’s examine the below directory /root/imagefiles/
[root@linuxnix imagefiles]# du -ah /root/imagefiles/ 20K /root/imagefiles/storage.state 4.0K /root/imagefiles/partdisk.txt 4.0K /root/imagefiles/osdisk.txt 84K /root/imagefiles/storage.log 4.0K /root/imagefiles/hdparm.out 120K /root/imagefiles/ks-script-dMBeha 4.0K /root/imagefiles/chompstr 4.0K /root/imagefiles/.treeinfo 4.0K /root/imagefiles/nwoptsw.txt 0 /root/imagefiles/nwparameters 4.0K /root/imagefiles/pre.log 56K /root/imagefiles/yum.log 144K /root/imagefiles/ks.cfg
Now, say we want to view this directory, but we are not interested in the .log file type, let’s do it one more time with the –exclude option
[root@linuxnix imagefiles]# du -ah --exclude=*log /root/imagefiles/ 20K /root/imagefiles/storage.state 4.0K /root/imagefiles/partdisk.txt 4.0K /root/imagefiles/osdisk.txt 4.0K /root/imagefiles/hdparm.out 120K /root/imagefiles/ks-script-dMBeha 4.0K /root/imagefiles/chompstr 4.0K /root/imagefiles/nwdisk.txt 4.0K /root/imagefiles/nwsystem 4.0K /root/imagefiles/nwpart.txt 28K /root/imagefiles/ks-script-EZvwhh
Example 8: We can even exclude files coming from different file systems. Examine the below File system, I am using /home as a file system and /home/rsasoc as a different file system. Suppose I want to check disk utilization of /home only, and ignore /home/rsasoc file system then use -x option.
[root@linuxnix ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup00-root 7.8G 6.7G 682M 91% / tmpfs 48G 0 48G 0% /dev/shm /dev/sda1 248M 59M 176M 26% /boot /dev/mapper/VolGroup00-usrhome 3.9G 8.1M 3.7G 1% /home /dev/mapper/VolGroup01-rsasoc 200G 69G 132G 35% /home/rsasoc /dev/mapper/VolGroup00-tmp 20G 526M 19G 3% /tmp /dev/mapper/VolGroup00-var 7.8G 434M 7.0G 6% /var /dev/mapper/VolGroup01-uax 200G 722M 200G 1% /var/lib/netwitness /dev/mapper/VolGroup00-rabmq 20G 36M 20G 1% /var/lib/rabbitmq /dev/mapper/VolGroup00-varlog 9.8G 1.1G 8.2G 12% /var/log /dev/mapper/VolGroup00-nwhome 30G 4.2G 26G 15% /var/netwitness /dev/mapper/VolGroup01-broker 200G 68G 133G 34% /var/netwitness/broker /dev/mapper/VolGroup01-redb 200G 71M 200G 1% /var/netwitness/database /dev/mapper/VolGroup00-ipdbext 30G 33M 30G 1% /var/netwitness/ipdbextractor /dev/mapper/VolGroup00-vartmp 3.9G 8.0M 3.7G 1% /var/tmp
[root@linuxnix ~]# du -hx /home 16K /home/lost+found 16K /home/cindy 0 /home/rsasoc 36K /home
Observer that /home/rsasoc is showing as zero, when it is actually 69 GB utilized.
Example 9: Combining du with sort, to sort the output based on maximum utilization.
Let’s now try to sort the output of du, so we check the maximum utilization files first.
du -sh /* 2> /dev/null | sort -rh
Where 2> /dev/null will direct any errors to /dev/null , sort -r will sort reversely “bigger size first”.
[root@linuxnix ~]# du -sh /* 2> /dev/null | sort -rh 74G /var 69G /home 4.4G /usr 1.1G /opt 666M /root 389M /tmp 258M /lib 57M /boot 37M /etc 27M /lib64 22M /share01 13M /sbin 6.2M /bin 2.7M /doc 252K /dev 16K /lost+found 4.0K /srv 4.0K /postinstall.log 4.0K /mnt 4.0K /media 0 /sys 0 /selinux 0 /proc 0 /mongodb-27017.sock
Example 10: I have hidden directories in my folder, how can I list only hidden folders size
du -hs .[!.]*
Let me explain what .[!.]* is all about. In order to understand this you should know . indicates present directory and .. indicates parent directory. what .[!.] indicates is my folder starts with . and it will not contain . again after the first dot. Which means we are effectively eliminating .. which is my parent directory. and .[!.]* is all the hidden files/folder in that given directory.
Example 11: Above example lists only hidden files and directories, how about list all folders/files in that directory.
du -hs .[!.]* *
Example output:
8.0K .thumbnails 1.4M .thunderbird 8.0K .vim 28K .viminfo 522M .wine 4.0K .Xauthority 4.0K .xsession-errors 4.0K .xsession-errors.old 2.0G 10.04_clean_image_for_12.04_16072015 4.0K 20150430-assets_rcvr.csv 4.0K 2.4.0-A2 load tests
Example 12: Some times we want to execute this command in many folders which is tedious job, how about making this as an alias so that the typing takes less time.
alias sm="du -hs * | grep -E ^[0-9]+M" alias sg="du -hs * | grep -E ^[0-9]+G"
The first command will list all the folders which are more than or equal to 1M and less than 1000M and second command will show all the files/folder which are more than or equal to 1G
Example: surendra@linuxnix:~$ sm 860M code 27M Music 26M Pictures 579M ubuntu-14.04.2-server-i386.iso surendra@linuxnix:~$ sg 16G Desktop
Example 13: If you want to round the way it counts to the nearest block size, so it acts in a way like df “we will get to this next”, you can use the –block-size option
[root@linuxnix cindy]# du -x --block-size=1K /home 16 /home/lost+found 20 /home/cindy 0 /home/rsasoc 40 /home
If you haven’t checked “df” command yet, please give it a look at our df command examples whereas du and df might seem similar in the way they show you your disk utilization, in fact there are a lots of differences.
Mostly, “du” will always count on files, and this will be the building block used to provide the total disk space utilized.In a sense of how many files that actually exist, and how they sum up to build this utilization. whereas “df” will always count on file system as the measure, and by that it will check the utilization it calculates for each file system mounted on a certain directory.
[root@linuxnix ~]# df -h /var Filesystem Size Used Avail Use% Mounted on /dev/sda1 452G 89G 359G 20% /
[root@linuxnix ~]# df -h / Filesystem Size Used Avail Use% Mounted on /dev/sda1 452G 89G 359G 20% /
Another huge misleading difference is du calculates the sum of separate file sizes, df is designed to count blocks used by file system. in a way that if block size is 1 KB “the default value”, if for instance a file is like 800 bytes utilizing one block. df will count the blocks and multiplies by 1024 to count, whereas again du will sum the actual size of each file, and outcomes a much more accurate number. Finally, in a world where all Linux file systems are Journaling, these journals are simply used disk space to keep track of files that are loaded in memory. this disk space used is not of your actual files but rather for the journal of your loaded files in memory. again df will calculate these [though they are not actually your files], whereas du will not. So basically in a nut shell and to sum up, if you are willing to get a big picture of how heavily your disk is utilized and ready to ignore willingly a margin or error, df command will be your tool, it is much faster than du and with much less processing. However if you are willing to be very granular, and in the scope of directories and the zoomed-in picture and willing to spend more time and CPU processing in return, then surely you will be using du and not df.
Latest posts by Surendra Anne (see all)
- Docker: How to copy files to/from docker container - June 30, 2020
- Anisble: ERROR! unexpected parameter type in action:
Fix - June 29, 2020 - FREE: JOIN OUR DEVOPS TELEGRAM GROUPS - August 2, 2019
- Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) - August 27, 2018
- How to use ohai/chef-shell to get node attributes - July 19, 2018