Tuesday, February 11, 2014

Find top 10 biggest files/directories from Linux shell

When disk is about full and I have to delete some files, I want to find the biggest file or directory and then probably delete it.

Following commands find the largest directories/files in current directory:
for i in $(ls); do du -s -h $i; done | sort -h  -r | head -n 10
 Sample output:
for i in $(ls); do du -s -h $i; done | sort -h  -r | head -n 10
4.9G    lib
231M    cache
23M     log
2.3M    backups
116K    spool
4.0K    tmp
4.0K    opt
4.0K    metrics
4.0K    mail
4.0K    local

Another similar command which is less helpful due to some duplicate info:

du -a -h /var | sort -h -r | head -n 10
Sample output:
# du -a -h /var | sort -h -r | head -n 10
5.1G    /var
4.9G    /var/lib
4.6G    /var/lib/libvirt/images/vn1.img
4.6G    /var/lib/libvirt/images
4.6G    /var/lib/libvirt
263M    /var/lib/apt/lists
263M    /var/lib/apt
231M    /var/cache
112M    /var/cache/pbuilder/aptcache
112M    /var/cache/pbuilder 

Note, the 2nd command is copied from here: http://www.cyberciti.biz/faq/how-do-i-find-the-largest-filesdirectories-on-a-linuxunixbsd-filesystem/

But I put -h  for both du and sort commands so that the result is easier to read (Otherwise, you will probably get a very long sequences of numbers). When  using -h, both du and sort need to be consistent, otherwise, it will give wrong output, like following:

# du -a -h /var | sort -r | head -n 10
988K    /var/cache/pbuilder/aptcache/libssl1.0.0_1.0.1-4ubuntu3_amd64.deb
976K    /var/lib/apt/lists/security.ubuntu.com_ubuntu_dists_raring-security_main_binary-amd64_Packages
96M     /var/cache/apt-xapian-index/index.1  ### should move up, and vn1.img is missing.

No comments: