How do I find the largest top 10 files and directories on a Linux / UNIX / BSD filesystem?

Sometime it is necessary to find out what file(s) or directories is eating up all disk space. Further it may be necessary to find out it at particular location such as /tmp or /var or /home etc.

There is no simple command available to find out the largest files/directories on a Linux/UNIX/BSD filesystem. However, combination of following three commands (using pipes) you can easily find out list of largest files:

  • du : Estimate file space usage
  • sort : Sort lines of text files or given input data
  • head : Output the first part of files i.e. to display first 10 largest file

Here is what you need to type at shell prompt to find out top 10 largest file/directories is taking up the most space in a /var directory/file system:
# du -a /var | sort -n -r | head -n 10
Output:

1008372 /var
313236  /var/www
253964  /var/log
192544  /var/lib
152628  /var/spool
152508  /var/spool/squid
136524  /var/spool/squid/00
95736   /var/log/mrtg.log
74688   /var/log/squid
62544   /var/cache

If you want more human readable output try:

# du -ks /var | sort -n -r | head -n 10

Where,

  • -a : Include all files, not just directories (du command)
  • -h : Human readable format
  • -n : Numeric sort (sort command)
  • -r : Reverse the result of comparisons (sort command)
  • -n 10 : Display 10 largest file. If you want 20 largest file replace 10 with 20. (head command)

Updated for accuracy!

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 15 comments… read them below or add one }

1 Anonymous 04.12.06 at 11:13 am

Great, but what if I only want the largest files and not the directories?

2 nixcraft 04.12.06 at 6:26 pm

To find out largest file only use command ls as follows in current directory:
ls -lSh . | head -5
Output:
-rw-r–r– 1 vivek vivek 267M 2004-08-04 15:37 WindowsXP-KB835935-SP2-ENU.exe
-rw-r–r– 1 vivek vivek 96M 2005-12-30 14:03 VMware-workstation-5.5.1-19175.tar.gz
ls -lSh /bin | head -5
You can also use find command but not du:
find /var -type f -ls | sort -k 7 -r -n | head -10

Hope this helps

3 nixcraft 04.12.06 at 6:35 pm

And yes to find the smallest files use command:
ls -lSr /var

Or use find command with -size flag.
find / -type f -size +20000k -exec ls -lh {} ; | awk ‘{ print $8 “: ” $5 }’

Read man page of find for more info.

4 john 01.19.07 at 7:43 pm

How do I can list all the files in several directories and at the end write the totat of all the files and directories.I’m using the du command as fallow:
du -sh /public/base/sites/F*/*20070115*

this command give me the size of all the files but not the global total.

can somebody help me. please write me. john_fernandez@verizon.com.do

5 Joe 01.23.07 at 12:49 am

“If you want more human readable output try:

# du -ha /var | sort -n -r | head -n 10″

Im pretty sure that this will put 999kb above 1gb so I don’t think that this works.

6 ChrisMM 04.17.07 at 7:26 am

This does not work.

# du -ha /var | sort -n -r | head -n 10″

as Joe says this ignores files over 1gb

7 Dreyser 05.27.07 at 11:32 pm

You could try this, gives a human readable output in MB

find . -type f | xargs ls -s | sort -rn | awk '{size=$1/1024; printf("%dMb %s\n", size,$2);}' | head

8 Benjamin Schmidt 09.26.07 at 6:32 pm

Human readable version:

for X in $(du -s * | sort -nr | cut -f 2); do du -hs $X ; done

9 RudyD 05.23.08 at 4:06 pm

If you set du to human readable I think it will not sort the way you really want.

For the above problems. I would like to find a way to list only the last level directories’ sizes.

(I want to filter somehow this:
/home
/home/user
/home/user/mail

I just want to see the lasts of the tree!)

TIA!


R

10 yanokwa 06.11.08 at 3:22 pm

this is what i use.

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

11 rschu68 09.19.08 at 1:46 pm

find . -type f -print0| xargs -0 ls -s | sort -rn | awk ‘{size=$1/1024; printf(”%dMb %s\n”, size,$2);}’ | head

! -print0 for filenames with spaces …
(and xargs -0 combined)

12 Winfan 04.14.09 at 12:04 am

No wonder windows rule

13 beez 06.19.09 at 10:31 pm

Winfan, I read this page wondering how to do these things in Windows — can you post instructions? Thanks!

14 sridevika lover 06.22.09 at 6:38 pm

thanks !!!!!!!!!!!!!!!!!

15 webmaster@@@@@@@@@@@ 06.22.09 at 6:39 pm

great job !!!!!!!!!!!

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post: My scripts in cgi-bin directory not working, how do I troubleshoot this problem?

Next post: How do I install a Perl Module?