Sometimes it may be required to find out which directory consuming how much disk space especially when you are used df -h
and realized your available disk space is low.
du:
du
command summarizes disk usage of the set of FILEs, recursively for directories.
It's often uses with -sh
option:
-s, --summarize
display only a total for each argument
-h, --human-readable
print sizes in human readable format (e.g., 1K 234M 2G)
For summarizing disk usages of the files in the current directory we use:
du -sh *
Example output:
572K Documents
208M Downloads
4,0K Music
724K Pictures
4,0K Public
4,0K Templates
4,0K Videos
We can also include hidden files with using:
du -sh .[!.]* *
Example output:
6,3M .atom
4,0K .bash_history
4,0K .bash_logout
8,0K .bashrc
350M .cache
195M .config
12K .dbus
4,0K .dmrc
44K .gconf
60K .gem
520K .gimp-2.8
28K .gnome
4,0K .ICEauthority
8,3M .local
8,0K .nano
404K .nv
36K .pki
4,0K .profile
8,0K .ssh
0 .sudo_as_admin_successful
4,0K .Xauthority
4,0K .xsession-errors
4,0K .xsession-errors.old
572K Documents
208M Downloads
4,0K Music
724K Pictures
4,0K Public
4,0K Templates
4,0K Videos
Thirdly, you can add total to the output by adding ,-c, option:
du -sch .[!.]* *
Result:
.
.
.
4,0K Templates
4,0K Videos
769M total
Most importantly using du
command properly on the root directory is a life saving action to find out what application/service or user is consuming your disk space wildly. For example, in case of a ridiculously low level of disk space availability for a web and mail server, the reason could be a spam attack to your mail service and you can diagnose it just by using du
command.
Investigate root directory for disk usage:
sudo du -sch /.[!.]* /*
Example output:
16K /.VolumeIcon.icns
24K /.VolumeIcon.png
13M /bin
57M /boot
4,0K /cdrom
620K /dev
13M /etc
779M /home
0 /initrd.img
406M /lib
3,9M /lib32
4,0K /lib64
16K /lost+found
4,0K /media
4,0K /mnt
367M /opt
du: cannot access '/proc/18221/task/18221/fd/4': No such file or directory
du: cannot access '/proc/18221/task/18221/fdinfo/4': No such file or directory
du: cannot access '/proc/18221/fd/4': No such file or directory
du: cannot access '/proc/18221/fdinfo/4': No such file or directory
0 /proc
20K /root
du: cannot access '/run/user/1000/gvfs': Permission denied
9,4M /run
13M /sbin
4,0K /srv
0 /sys
72K /tmp
3,5G /usr
639M /var
0 /vmlinuz
5,8G total
Lastly, the best method forms when you add a threshold size value for directories to ignore small ones. This command will only show folders with more than 1GB in size which located under root directory up to the farthermost branch of the whole directory tree in your file system:
sudo du --threshold=1G -ch /.[!.]* /*
Example output:
1,4G /usr/lib
1,8G /usr/share
3,5G /usr
5,8G total