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 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 Beispiels
Execute du command with no options. As you can see in this Beispiel, 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
[[email protected] mnt /]# du /opt 5 /opt/rh 7 /opt
If you dont 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.
[[email protected] mnt]# du -h /mnt 200K /mnt/dir1 204K /mnt
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.
[[email protected] mnt ~]# du -sh /var 1.41G /var
Or even one level down than just /var to include sub-directories
[[email protected] mnt ~]# du -sh /var/\* 5.0K /var/account 251M /var/cache 5.0K /var/crash 5.0K /var/cvs 5.0K /var/nis 5.0K /var/opt 5.0K /var/preserve 384K /var/run 73M /var/spool 160K /var/tmp 499M /var/www 12K /var/yp
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.
[[email protected] 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
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.
[[email protected] mnt mnt\]# du -ah --max-depth=1 /var/ 3.0K /var/local 3.0K /var/crash 149K /var/tmp 4.3K /var/opt 335K /var/run 25K /var/lock 0.1K /var/mail 5.0K /var/account 150M /var/lib 700M /var/www
Now lets do the same as previous Beispiel, but add the –time option this will increase your visibility, it will show you a new column with the time last modified shown.
[[email protected] mnt]# du -ah --max-depth=1 --time /var/ 5.0K 2015-09-23 13:50 /var/local 12K 2015-08-07 18:38 /var/yp 5.0K 2015-01-06 13:36 /var/crash 160K 2015-02-08 11:49 /var/tmp 5.0K 2015-09-23 13:50 /var/opt 384K 2015-03-03 17:46 /var/run 24K 2015-03-03 03:50 /var/lock 0 2015-08-07 18:35 /var/mail 5.0K 2015-08-07 18:46 /var/account 145M 2015-03-03 19:09 /var/lib 699M 2015-02-28 09:10 /var/www 5.0K 2015-09-23 13:50 /var/nis 49M 2015-03-03 19:14 /var/log 1.1G 2015-03-03 19:14 /var/
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/
[[email protected] imagefiles]# du -ah /root/imagefiles/ 20K /root/imagefiles/storage.state 5.0K /root/imagefiles/partdisk.txt 5.0K /root/imagefiles/osdisk.txt 84K /root/imagefiles/storage.log 5.0K /root/imagefiles/hdparm.out 120K /root/imagefiles/ks-script-dMBeha 5.0K /root/imagefiles/chompstr 5.0K /root/imagefiles/.treeinfo 5.0K /root/imagefiles/nwoptsw.txt 0 /root/imagefiles/nwparameters 5.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
[[email protected] imagefiles]# du -ah --exclude=*log /root/imagefiles/ 23K /root/imagefiles/storage.state 5.0K /root/imagefiles/partdisk.txt 5.0K /root/imagefiles/osdisk.txt 5.0K /root/imagefiles/hdparm.out 120K /root/imagefiles/ks-script-dMBeha 5.0K /root/imagefiles/chompstr 5.0K /root/imagefiles/nwdisk.txt 5.0K /root/imagefiles/nwsystem 5.0K /root/imagefiles/nwpart.txt 28K /root/imagefiles/ks-script-EZvwhh
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.
[[email protected] ~]# 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 [[email protected] ~]# du -hx /home 19K /home/lost+found 19K /home/user 10 /home/lxu 36K /home
Observer that /home/rsasoc is showing as zero, when it is actually 69 GB utilized.
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”.
[[email protected] ~]# 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 5.0K /srv 5.0K /postinstall.log 5.0K /mnt 5.0K /media 0 /sys 0 /selinux 0 /proc 0 /mongodb-27017.sock
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.
Above Beispiel lists only hidden files and directories, how about list all folders/files in that directory.
du -hs .[!.]* * 8.0K .thumbnails 1.4M .thunderbird 8.0K .vim 28K .viminfo 522M .wine 5.0K .Xauthority 5.0K .xsession-errors 5.0K .xsession-errors.old 2.0G 10.04_clean_image_for_12.04_16072021 5.0K 20210430-assets_rcvr.csv 5.0K 2.4.0-A2 load tests
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
[[email protected]]#~$ sm 863M codeing 273M Music 262M Pictures 559M ubuntu-14.04.2-server-i386.iso [[email protected]]#~$ sg 16G Desktop
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
[[email protected] 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 Beispiels whereas du and df might seem similar in the way they show you your disk utilization, in fact there are 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.
[[email protected] ~]# df -h /var Filesystem Size Used Avail Use% Mounted on /dev/sda1 452G 89G 359G 20% / [[email protected] ~]# 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.
The Command df “disk file system” is one of the very powerful tools every system admin should know. when mastering this command you should understand lot of information about your local and remote file system details like mount point, disk utilization, partitions etc. In this post, we will find out some of the very common and useful ways to use “df” to monitor your file system. The df command read its output from /proc/mounts file and a kernel function statfs(2). This file /proc/mounts give you where block files are mounted and statfs(2) will help you query kernel to get present disk sizes. The df command will combine these two and print some meaningful information for a Linux user.
Basic df command usage Beispiele
Using the “df” without any options. If you use the df command without any arguments, it will show you something like below. You will get columns of File system, number of blocks in 1K each, number of used blocks, number of available blocks, used percentage and finally the mount point for each file system. As you will get to know this command better, you will come to know that it is handier to mix it with some options, we will get to that later. You should know about what is block size in Linux before proceeding.
[[email protected]]/proc# df Filesystem 1K-blocks Used Available Use% Mounted on udev 3961912 4 3961908 1% /dev tmpfs 794540 1372 793168 1% /run /dev/sdb1 22951396 14653116 7109368 68% / none 4 0 4 0% /sys/fs/cgroup none 5120 0 5120 0% /run/lock none 3972696 385424 3587272 10% /run/shm none 102400 64 102336 1% /run/user /dev/sda1 99800 3440 96361 4% /boot/efi /dev/sda4 849698404 239254192 567258908 30% /opt /dev/sda2 95990540 73446284 17645084 81% /home /dev/mmcblk0p1 3871744 1880480 1991264 49% /media/surendra/9016-4EF8
Before going in to Beispiels, we will try to decode some of the above lines.
Understanding normal mount mappings
/dev/sdb1 22G 15G 6.8G 68% /
/dev/sdb1 partition is mounted on / whose size is 22G, used is 14Gb and available is 6.8Gb.
Understanding special mount mappings.
udev 3.8G 5.0K 3.8G 1% /dev
Some times, We have to mount some folders in special partitions which are virtual in nature. This will help in preventing accidental deletion of important data for system to work properly. The above Beispiel /dev folder which contain device file information and may vary with every reboot is actually mounted with a virtual file system.
Understanding below line
tmpfs 794550 13742 79318 1% /run
The /run folder is recent implementation which contain process related information and which should not be deleted by any one until next reboot.
If you observe below line, the virtual partition is none and mounted on /run/user. In other words this is tactfully virtual file system with the virtual file system, because /run is using tmpfs virtual partition.
none 10240 64 10236 1% /run/user
Do you know we can mount remote file systems like Samba, NFS, SSH and FTP? Below are Beispiel lines you may see in df command output.
Mounting FTP locally
“df -T” output line for ftp mount/share
curlftpfs# ftp://sandra_a:[email protected]/ 7.5T 0 7.5T 0% /ftpmount
Mounting SSH locally
“df -T” output line for SSH mount/share
[[email protected]]# /root fuse.sshfs 20G 4.0G 15G 22% /mnt
Mounting NFS locally
“df -T” output line for NFS mount/share
abc.linuxnix.com:/home nfs 100G 1G 99G 1% /mnt/nfs/home
Mounting Samba locally
“df -T” output line for samba mount/share
//surtr/Files smbfs 254G 140G 115G 55% /mnt/Files
Let us start exploring df command with options. The first in the list is -h option. As you can see, using the df command without any options can list you the file system usage, however it lists the size for each file system in number of blocks. A better way to look at it, is to print the size in a more human readable format, and this is what the -h option does. As you notice below, the -h option changed the way the size column looks like. It is most likely that you will be using the df -h “then combine it with more options” than using just df.
[[email protected]]#/proc# df -h Filesystem Size Used Avail Use% Mounted on udev 3.8G 5.0K 3.8G 1% /dev tmpfs 776M 1.4M 775M 1% /run /dev/sdb1 22G 14G 6.8G 68% / none 5.0K 0 5.0K 0% /sys/fs/cgroup none 5.0M 0 5.0M 0% /run/lock none 3.8G 349M 3.5G 9% /run/shm none 100M 64K 100M 1% /run/user /dev/sda1 98M 3.4M 95M 4% /boot/efi /dev/sda4 811G 229G 541G 30% /opt /dev/sda2 92G 71G 17G 81% /home /dev/mmcblk0p1 3.7G 1.8G 1.9G 49% /media/surendra/9016-4EF8 /root 20G 4.0G 15G 22% /mnt
Using the “df” with -T option. Moving on now, the -T options, which prints the type of each file system, and by that it means the format of each file system. As you can notice below, adding the -T option, will add a new column which is the file system type.
[[email protected]]#/proc# df -T Filesystem Type 1K-blocks Used Available Use% Mounted on udev devtmpfs 3961912 4 3961908 1% /dev tmpfs tmpfs 794540 1368 793172 1% /run /dev/sdb1 ext4 22951396 14653560 7108924 68% / none tmpfs 4 0 4 0% /sys/fs/cgroup none tmpfs 5120 0 5120 0% /run/lock none tmpfs 3972696 356728 3615968 9% /run/shm none tmpfs 102400 64 102336 1% /run/user /dev/sda1 vfat 99800 3440 96361 4% /boot/efi /dev/sda4 ext4 849698404 239254192 567258908 30% /opt /dev/sda2 ext4 95990540 73456020 17635348 81% /home /dev/mmcblk0p1 vfat 3871744 1880480 1991264 49% /media/surendra/9016-4EF8 /root fuse.sshfs 20511356 4181644 15264756 22% /mnt
Using the “df” with -t option. Notice that Linux is case sensitive, the -t options will only list file system with the type you specified in the arguments. This will not print any other file systems which are mounted at present.
[[email protected]\]/proc# df -t fuse.sshfs Filesystem 1K-blocks Used Available Use% Mounted on /root 20511356 4181672 15264728 22% /mnt
Note that you can combine the -t with -h for a human readable view, however, make sure that the -t option is the last option facing the argument “the file system type”. In other words, it should be “df -ht ext3” and not “df -th ext3”.
[[email protected]]#/proc# df -ht vfat Filesystem Size Used Avail Use% Mounted on /dev/sda1 98M 3.4M 95M 4% /boot/efi /dev/mmcblk0p1 3.7G 1.8G 1.9G 49% /media/sandra/9016-4EF8
Using the “df” with -x option. The -x option, excludes any file system with the given type in the arguments, in other words, it will print all file systems, that do not match the file system type you mentioned. This is very neat when you want to focus on file systems and the one you are mentioning is not interesting for you. The below Beispiel shows how to view all file system that are not tmpfs type.
[[email protected]]#/proc# df -hTx tmpfs Filesystem Type Size Used Avail Use% Mounted on udev devtmpfs 3.8G 5.0K 3.8G 1% /dev /dev/sdb1 ext4 22G 14G 6.8G 68% / /dev/sda1 vfat 98M 3.4M 95M 4% /boot/efi /dev/sda4 ext4 811G 229G 541G 30% /opt /dev/sda2 ext4 92G 71G 17G 81% /home /dev/mmcblk0p1 vfat 3.7G 1.8G 1.9G 49% /media/surendra/9016-4EF8 fuse.sshfs 20G 4.0G 15G 22% /mnt
As you are getting now, we have to make the -x option facing the “tmpfs” which is the argument. “df -Thx tmpfs” will also work in the same fashion, however “df -xhT tmpfs” will not work.
Using the “df” with -i option to print inode details. Another way to view the utilization of your system is to check the number of inodes utilized by each file system. A side note is that for each file system type, file system might become full even if there is still space left, and that is because it reached the maximum number of inodes the inode table can take. This is depending on the file system type. So in some cases, it is handy to know the number of inodes utilized by each file system.
[[email protected]]#/proc# df -i Filesystem Inodes IUsed IFree IUse% Mounted on udev 990478 567 989911 1% /dev tmpfs 993174 642 992532 1% /run /dev/sdb1 1466368 698227 768141 48% / none 993174 11 993163 1% /sys/fs/cgroup none 993174 3 993171 1% /run/lock none 993174 342 992832 1% /run/shm none 993174 32 993142 1% /run/user /dev/sda1 0 0 0 - /boot/efi /dev/sda4 53968896 11752 53957144 1% /opt /dev/sda2 6111232 46688 6064544 1% /home /dev/mmcblk0p1 0 0 0 - /media/surendra/9016-4EF8 /root 1310720 124303 1186417 10% /mnt
Note: Now columns represent different stuff. Second column represents total number of Inodes, third used, fourth free inodes and fifth shows % used inodes.
Using the “df” with a certain directory in the arguments. One final thing to mention about df , is that you can use it on a specific directory. Although it will not show the directory utilization “other commands can do this”, but it will show you which file system this directory is part of, and the utilization for the file system to which the directory belongs. it is still handy to have a big picture of how much space left you can fill up this directory.
[[email protected]]#/proc# df -hT /home Filesystem Type Size Used Avail Use% Mounted on /dev/sda2 ext4 92G 71G 17G 81% /home
List only local partitions and mount points.
[[email protected]]/proc# df -l Filesystem 1K-blocks Used Available Use% Mounted on udev 3961912 4 3961908 1% /dev tmpfs 794540 1332 793208 1% /run /dev/sdb1 22951396 14653660 7108824 68% / none 4 0 4 0% /sys/fs/cgroup none 5120 0 5120 0% /run/lock none 3972696 342676 3630020 9% /run/shm none 102400 64 102336 1% /run/user /dev/sda1 99800 3440 96361 4% /boot/efi /dev/sda4 849698404 239254192 567258908 30% /opt /dev/sda2 95990540 73449484 17641884 81% /home /dev/mmcblk0p1 3871744 1880480 1991264 49% /media/sandra/9016-4EF8
Display all available mount points.
[[email protected]]#/proc# df -a Filesystem 1K-blocks Used Available Use% Mounted on sysfs 0 0 0 - /sys proc 0 0 0 - /proc udev 3961912 4 3961908 1% /dev devpts 0 0 0 - /dev/pts tmpfs 794540 1332 793208 1% /run /dev/sdb1 22951396 14653652 7108832 68% / none 4 0 4 0% /sys/fs/cgroup none 0 0 0 - /sys/fs/fuse/connections none 0 0 0 - /sys/kernel/debug none 0 0 0 - /sys/kernel/security cgroup 0 0 0 - /sys/fs/cgroup/cpuset cgroup 0 0 0 - /sys/fs/cgroup/cpu cgroup 0 0 0 - /sys/fs/cgroup/cpuacct cgroup 0 0 0 - /sys/fs/cgroup/memory cgroup 0 0 0 - /sys/fs/cgroup/devices cgroup 0 0 0 - /sys/fs/cgroup/freezer cgroup 0 0 0 - /sys/fs/cgroup/blkio cgroup 0 0 0 - /sys/fs/cgroup/perf\_event cgroup 0 0 0 - /sys/fs/cgroup/hugetlb none 0 0 0 - /sys/firmware/efi/efivars none 5120 0 5120 0% /run/lock none 3972696 342776 3629920 9% /run/shm none 102400 64 102336 1% /run/user none 0 0 0 - /sys/fs/pstore /dev/sda1 99800 3440 96361 4% /boot/efi /dev/sda4 849698404 239254192 567258908 30% /opt /dev/sda2 95990540 73449556 17641812 81% /home systemd 0 0 0 - /sys/fs/cgroup/systemd /dev/sdb1 22951396 14653652 7108832 68% /var/lib/docker/aufs gvfsd-fuse 0 0 0 - /run/user/1000/gvfs /dev/mmcblk0p1 3871744 1880480 1991264 49% /media/sandra/9016-4EF8 /root 20511356 4183856 15262544 22% /mnt
Display POSIX standard format which is useful when writing shell scripts.
[[email protected]]/proc# df -P Filesystem 1024-blocks Used Available Capacity Mounted on udev 3961912 4 3961908 1% /dev tmpfs 794540 1332 793208 1% /run /dev/sdb1 22951396 14653672 7108812 68% / none 4 0 4 0% /sys/fs/cgroup none 5120 0 5120 0% /run/lock none 3972696 342772 3629924 9% /run/shm none 102400 64 102336 1% /run/user /dev/sda1 99800 3440 96361 4% /boot/efi /dev/sda4 849698404 239254192 567258908 30% /opt /dev/sda2 95990540 73449112 17642256 81% /home /dev/mmcblk0p1 3871744 1880480 1991264 49% /media/ssandra/9016-4EF8 /root 20511356 4183936 15262464 22% /mnt
Did not like any of the formats and want your own format? Do not worry, df have –output option which support below fields.
Valid field names are: ‘source’, ‘fstype’, ‘itotal’, ‘iused’, ‘iavail’, ‘ipcent’, ‘size’, ‘used’, ‘avail’, ‘pcent’ and ‘target’
Suppose, I want to see partition and just mount point, use below Beispiel.
[[email protected]]/proc# df --output=source,target Filesystem Mounted on udev /dev tmpfs /run /dev/sdb1 / none /sys/fs/cgroup none /run/lock none /run/shm none /run/user /dev/sda1 /boot/efi /dev/sda4 /opt /dev/sda2 /home /dev/mmcblk0p1 /media/sandra/9016-4EF8 /root /mnt
In our next post we will see about dd command.