Linux Administrator

Find Top 10 Space Consuming Directories & Files On Linux & UNIX System

As Linux administrator, sometimes we need to know what directories and files are eating up all our disk space. Similarly, we should be able to discover a particular directory location on file system such /var, /tmp, /opt and /home etc.

In this article I will try to help you, to find top 10 space consuming directories and files in Linux system.

Although, there is no any shortcut command to find out the top space consuming directories and files on Linux system. However, We can use following three commands with pipes to do this.

  • du :- It estimates file space usage.
  • sort :- Sort lines of text files or given input data.
  • head :- Display first 10 files and output the first part of files.
  • find :- To search file.

Find Top 10 Space Consuming Directories and Files

Execute the below command at the shell prompt to find out top largest Files and Directories.

# du -a /var/spool/ | sort -n -r | head -n 10

You will get some output like below.

16888 /var/spool/
13916 /var/spool/mqueue
2928 /var/spool/mail
2924 /var/spool/mail/root
16 /var/spool/anacron
8 /var/spool/cron
4 /var/spool/samba
4 /var/spool/plymouth
4 /var/spool/lpd
4 /var/spool/cron/root

OR, If you want output in human readable format follow the below command.

# cd /var/spool/
# du -hsx * | sort -rh | head -10

You will get some output like below.

14M mqueue
2.9M mail
16K anacron
8.0K cron
4.0K samba
4.0K plymouth
4.0K lpd
4.0K clientmqueue

Where;

  • du -h : Display sizes in human readable format.
  • du -s : Show total for each argument.
  • du -x : Skip directoried on different file system.
  • sort -r : Reverse the result of comparisons.
  • sort -h : Compare human readable numbers.
  • head -10 : Display the first 10 lines.
    OR
  • head -n10 : Display the first 10 lines.

You can use above command only if GNU/sort is installed. For other UNIX systems should use the below command.

# for i in G M K; do du -ah | grep [0-9]$i | sort -nr -k 1; done | head -n 11

17M .
14M ./mqueue
2.9M ./mail/root
2.9M ./mail
16K ./anacron
8.0K ./cron
4.0K ./samba
4.0K ./plymouth
4.0K ./lpd
4.0K ./cron/root
4.0K ./clientmqueue

Find top 10 space consuming files in a dirctory and its subdirectories

Follow the below GNU/find command.

# cd /var/www/web3/web/chat/
# find . -printf '%s %p\n'| sort -nr | head -10

978882 ./img/home/live2support_home_page.JPG
255488 ./images/crm/Thumbs.db
198535 ./images/integrations/core5.jpg
198535 ./images/e-commerce/core5.jpg
193173 ./img/table_tab.png
191214 ./images/e-commerce/ameri4.jpg
186588 ./images/e-commerce/ameri5.jpg
177152 ./images/awards/Thumbs.db
167702 ./images/e-commerce/ameri2.jpg
163989 ./images/integrations/BrandonHallGrp.png

OR

# find /var/www/web3/web/chat/ -printf '%s %p\n'| sort -nr | head -10

978882 /var/www/web3/web/chat/img/home/live2support_home_page.JPG
255488 /var/www/web3/web/chat/images/crm/Thumbs.db
198535 /var/www/web3/web/chat/images/integrations/core5.jpg
198535 /var/www/web3/web/chat/images/e-commerce/core5.jpg
193173 /var/www/web3/web/chat/img/table_tab.png
191214 /var/www/web3/web/chat/images/e-commerce/ameri4.jpg
186588 /var/www/web3/web/chat/images/e-commerce/ameri5.jpg
177152 /var/www/web3/web/chat/images/awards/Thumbs.db
167702 /var/www/web3/web/chat/images/e-commerce/ameri2.jpg
163989 /var/www/web3/web/chat/images/integrations/BrandonHallGrp.png

Skip the directories and only show files

You can also skip the directories and only show files , follow the below command.

# find /var/www/web3/web/chat/ -type f -printf '%s %p\n'| sort -nr | head -10

978882 /var/www/web3/web/chat/img/home/live2support_home_page.JPG
255488 /var/www/web3/web/chat/images/crm/Thumbs.db
198535 /var/www/web3/web/chat/images/integrations/core5.jpg
198535 /var/www/web3/web/chat/images/e-commerce/core5.jpg
193173 /var/www/web3/web/chat/img/table_tab.png
191214 /var/www/web3/web/chat/images/e-commerce/ameri4.jpg
186588 /var/www/web3/web/chat/images/e-commerce/ameri5.jpg
177152 /var/www/web3/web/chat/images/awards/Thumbs.db
167702 /var/www/web3/web/chat/images/e-commerce/ameri2.jpg
163989 /var/www/web3/web/chat/images/integrations/BrandonHallGrp.png

OR

# find /var/www/web3/web/chat/ -type f -iname "*.jpg" -printf '%s %p\n'| sort -nr | head -10

978882 /var/www/web3/web/chat/img/home/live2support_home_page.JPG
198535 /var/www/web3/web/chat/images/integrations/core5.jpg
198535 /var/www/web3/web/chat/images/e-commerce/core5.jpg
191214 /var/www/web3/web/chat/images/e-commerce/ameri4.jpg
186588 /var/www/web3/web/chat/images/e-commerce/ameri5.jpg
167702 /var/www/web3/web/chat/images/e-commerce/ameri2.jpg
156252 /var/www/web3/web/chat/images/e-commerce/big5.jpg
139324 /var/www/web3/web/chat/images/e-commerce/ameri3.jpg
138477 /var/www/web3/web/chat/images/e-commerce/simg2.jpg
129080 /var/www/web3/web/chat/images/crm/infu8.jpg

I hope this article will help you to find out the top space consuming directories and files on Linux & UNIX system. If you have any queries and problem please comment in comment section.

Thanks:)

Thank you! for visiting LookLinux.

If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at LookLinux Submit Article. Stay connected to Facebook.

About the author

mm

Santosh Prasad

Hi! I'm Santosh and I'm here to post some cool article for you. If you have any query and suggestion please comment in comment section.

2 Comments

Leave a Comment