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

by nixcraft on April 3, 2006 · 29 comments

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!

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 29 comments… read them below or add one }

1 Anonymous April 12, 2006

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

Reply

2 nixcraft April 12, 2006

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

Reply

3 nixcraft April 12, 2006

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.

Reply

4 Spechal June 22, 2011

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

needs to have the exec altered

find / -type f -size +20000k -exec ls -lh {} \; | awk ‘{ print $8 “: ” $5 }’

Also, I find this output easier to read

find . -type f -size +20000k -exec ls -lh {} \; | awk ‘{print $5″: “$8}’

Reply

5 john January 19, 2007

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

Reply

6 Joe January 23, 2007

“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.

Reply

7 ChrisMM April 17, 2007

This does not work.

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

as Joe says this ignores files over 1gb

Reply

8 Dreyser May 27, 2007

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

Reply

9 Benjamin Schmidt September 26, 2007

Human readable version:

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

Reply

10 RudyD May 23, 2008

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

Reply

11 yanokwa June 11, 2008

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

Reply

12 rschu68 September 19, 2008

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)

Reply

13 Winfan April 14, 2009

No wonder windows rule

Reply

14 Mr-Yellow July 26, 2011

Yeah you just go ahead and wait the 3 hours this search would take on Windows.

Reply

15 Nick November 30, 2011

How about the 3 hours it takes to read through a bunch of unexplained programming nonsense. I swear half of the time all Linux guys do is insult other users….Example, the first listing is great as it begins to explain what the flags do, but I have no idea were to put some of them, pipes are not explained…ect

Reply

16 beez June 19, 2009

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

Reply

17 Dave January 18, 2011

This may vary depending on the version of Windows you’re using, but the basic procedure is: open the find/search window, go to the advanced options, and there will be an option there to enter in a size parameter. Simple.

In XP, press F3 or go Start->Search. Choose “All files and folders”, then “More advanced options”, then “What size is it?”, then specify a size.

Reply

18 sridevika lover June 22, 2009

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

Reply

19 webmaster@@@@@@@@@@@ June 22, 2009

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

Reply

20 notafan August 21, 2009

@winfan… how do you do this in windows? you don’t :P

Reply

21 Jayman September 12, 2011

c:\>dir /S /O-S | more

The simple dir /S command from c:\ will give you all files and directories from c:\ all the way through the drive and will sort from largest to smallest running through each directory. You can filter using /A if you’d like to restrict by hidden, system, archive files, read only files etc. and passing the output to another windows command if you need to further restrict or search in the files for something like “show me all the files on my hard drive over 6MB that contain the word ‘log’ from largest to smallest.”

/O will Specify the sort order for the files with the S after it sorting by file size (smallest first) putting the – in front of the S reverses the order.

| more – you’re a unix dude, you should know what this means…

But if someone is doing some cleanup through their harddrive, this is the simple command I’d start with.

Just a note about the cockyness or us Unix admins (as I happen to be one now)
Not everyone that uses windows started using it with a mouse kid!!! Also not everyone who prefers windows is not cross-platform… We were running 64 bit clustered NT boxes on RISC processors at Digital Equipment Corporation with failover and failback in 1996 brother. Don’t believe me? Find a really old copy of Windows NT ver 3.51 open it and you’ll see two folders NT and Alpha.

The Department of Veterans Affairs had no problems with ever needing to reboot a “lousy unreliable windows box” because the Intel platform itself was the problem, not windows. We ran Alpha on 64 bit RISC processors and it was just as reliable as any Unix box or Mainframe we had. I had a Jensen Alpha running an exchange server for 5 years, and we only rebooted it every 6 months for giggles…

Windows machines are made to be used by the masses which means more dumbasses can kinda run one. A good Admin is a good Admin, no matter what platform. Be nice and be helpful or don’t post.

Reply

22 Tihamer January 20, 2010

Notafan wrote:
@winfan… how do you do this in windows? you don’t :P

Well, actually, there *is* cygwin (unix commands for Windows systems)
http://www.cygwin.com/

Reply

23 Keith White April 7, 2010

I find the following works rather well…

du -xak . | sort -n | awk '{size=$1/1024; path=""; for (i=2; i 50) { printf("%dMb %s\n", size,path); } }'

It lists all files or directories bigger than 50MB (just change size>50 to alter that) in the current directory (change the “.” to a directory path to specify another one) in a friendly, human-readable way and happily plays with spaces (I used it on an NTFS mount in fact).

Reply

24 Brian March 10, 2011

I get a syntax error when coptying and pasting this command

Reply

25 Thomas December 21, 2011

Same here…

Reply

26 Rafiq February 18, 2011

I found this one the simplest one.
ls -lSh . | head -5

Reply

27 Satish June 13, 2011

I have learnt a lot from your posts

thanks alot and keep it up.

Reply

28 aaa October 4, 2011

ls -lR | sort -nr +4 -5 | head

Reply

29 Nalinda December 14, 2011

Thanks heaps for this. It saved me lot of time.

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 14 + 6 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the simple math so we know that you are a human and not a script.



Previous post:

Next post: