How Do I Find The Largest Top 10 Files and Directories On a Linux / UNIX / BSD?

by on April 3, 2006 · 43 comments· LAST UPDATED July 30, 2014

in , ,

How do I find the largest top files and directories on a Linux or Unix like operating systems?

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

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:

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
RequirementsNone
Estimated completion time1m

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

Type the following command at the shell prompt to find out top 10 largest file/directories:
# du -a /var | sort -n -r | head -n 10
Sample outputs:

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:
$ cd /path/to/some/where
$ du -hsx * | sort -rh | head -10

Where,

  • du command -h option : display sizes in human readable format (e.g., 1K, 234M, 2G).
  • du command -s option : show only a total for each argument (summary).
  • du command -x option : skip directories on different file systems.
  • sort command -r option : reverse the result of comparisons.
  • sort command -h option : compare human readable numbers. This is GNU sort specific option only.
  • head command -10 OR -n 10 option : show the first 10 lines.

The above command will only work of GNU/sort is installed. Other Unix like operating system should use the following version (see comments below):

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

Sample outputs:

179M	.
84M	./uploads
57M	./images
51M	./images/faq
49M	./images/faq/2013
48M	./uploads/cms
37M	./videos/faq/2013/12
37M	./videos/faq/2013
37M	./videos/faq
37M	./videos
36M	./uploads/faq

Find the largest file in a directory and its subdirectories using the find command

Type the following GNU/find command:

## Warning: only works with GNU find ##
find /path/to/dir/ -printf '%s %p\n'| sort -nr | head -10
find . -printf '%s %p\n'| sort -nr | head -10
 

Sample outputs:

5700875 ./images/faq/2013/11/iftop-outputs.gif
5459671 ./videos/faq/2013/12/glances/glances.webm
5091119 ./videos/faq/2013/12/glances/glances.ogv
4706278 ./images/faq/2013/09/cyberciti.biz.linux.wallpapers_r0x1.tar.gz
3911341 ./videos/faq/2013/12/vim-exit/vim-exit.ogv
3640181 ./videos/faq/2013/12/python-subprocess/python-subprocess.webm
3571712 ./images/faq/2013/12/glances-demo-large.gif
3222684 ./videos/faq/2013/12/vim-exit/vim-exit.mp4
3198164 ./videos/faq/2013/12/python-subprocess/python-subprocess.ogv
3056537 ./images/faq/2013/08/debian-as-parent-distribution.png.bak

You can skip directories and only display files, type:

 
find /path/to/search/ -type f -printf '%s %p\n'| sort -nr | head -10
 

OR

 
find /path/to/search/ -type f -iname "*.mp4" -printf '%s %p\n'| sort -nr | head -10
 

Hunt down disk space hogs with ducks

Use the following bash shell alias:

 
alias ducks='du -cks * | sort -rn | head'
 

Run it as follows to get top 10 files/dirs eating your disk space:
$ ducks

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 43 comments… read them below or add one }

1 Anonymous April 12, 2006 at 11:13 am

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

Reply

2 nixcraft April 12, 2006 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

Reply

3 nixcraft April 12, 2006 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.

Reply

4 Spechal June 22, 2011 at 5:47 pm

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

Reply

6 Joe January 23, 2007 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.

Reply

7 ChrisMM April 17, 2007 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

Reply

8 Dreyser May 27, 2007 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

Reply

9 Benjamin Schmidt September 26, 2007 at 6:32 pm

Human readable version:

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

Reply

10 RudyD May 23, 2008 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

Reply

11 yanokwa June 11, 2008 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

Reply

12 Srinivas October 25, 2013 at 4:56 pm

Worked like charm

Reply

13 rschu68 September 19, 2008 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)

Reply

14 Winfan April 14, 2009 at 12:04 am

No wonder windows rule

Reply

15 Mr-Yellow July 26, 2011 at 12:54 am

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

Reply

16 Nick November 30, 2011 at 1:56 pm

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

17 beez June 19, 2009 at 10:31 pm

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

Reply

18 Dave January 18, 2011 at 5:56 pm

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

19 SamTzu August 6, 2013 at 4:35 pm

I believe beez was being sarcastic towards Winfan as was his right after Winfans brainless comment. Most Linux distro GUI’s come with search function just like Windows GUI.

Now just try to do the search on command line on Windows (server)…

Sam

Reply

20 sridevika lover June 22, 2009 at 6:38 pm

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

Reply

21 webmaster@@@@@@@@@@@ June 22, 2009 at 6:39 pm

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

Reply

22 notafan August 21, 2009 at 9:54 pm

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

Reply

23 Jayman September 12, 2011 at 8:31 pm

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

24 Tihamer January 20, 2010 at 6:08 pm

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

25 Keith White April 7, 2010 at 10:44 am

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

26 Brian March 10, 2011 at 3:00 am

I get a syntax error when coptying and pasting this command

Reply

27 Thomas December 21, 2011 at 11:19 am

Same here…

Reply

28 Rafiq February 18, 2011 at 8:51 pm

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

Reply

29 Satish June 13, 2011 at 10:22 pm

I have learnt a lot from your posts

thanks alot and keep it up.

Reply

30 aaa October 4, 2011 at 2:44 pm

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

Reply

31 Nalinda December 14, 2011 at 11:29 pm

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

Reply

32 Steve August 22, 2012 at 1:14 pm

The easiest way would be
ls -lSh | head -n 10

Reply

33 tobeportable March 10, 2013 at 12:06 pm

install `ncdu`

Reply

34 Avi April 18, 2013 at 2:50 pm

I second that! Try NCDU, it’s command-line and gives you interactive folder listings.

Reply

35 remke April 18, 2013 at 7:43 pm

dispus
-> dispus2.7 (google)

With kind regards,
~remke

Reply

36 Azad June 4, 2013 at 11:56 am

How to go at last line at a big file directly by using the unix command. cud u plz assist me for the same @ ibm_guy2000@yahoo.com

Reply

37 karthik July 13, 2013 at 12:35 am

Hi
How can i find the largest file size in centos ..
and also through dates and all the details of the file.

Thanks..

Reply

38 Anjana.S July 21, 2013 at 7:55 am

how can i open a human readable file from my current directory?

Reply

39 shirish October 13, 2013 at 4:30 am

hows this !!!
##top 20
# find /var -ls 2>/dev/null | sort -nrk 7 | awk ‘{print $NF}’ | xargs ls -lSh | head -20

Reply

40 Christian Benesch November 8, 2013 at 1:00 pm

The following is working and sorting properly by Gigabyte, Megabytes and Kilobytes:

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

Basically it makes sure to grep from the *beginning* of the line and to include the possible decimal points. In your version it would have stumbled over filenames beginning with numbers and a ‘G’ for instance.

Reply

41 Suraj November 15, 2013 at 3:56 pm

I need to display only the name of the file with maximum size in home directory…please help..

Reply

42 Silverback December 3, 2013 at 8:06 pm
find / -xdev -size +2048 -ls  

List all files larger than 1 mb. Displays

1632043 2656 -rw-r-----   1 root     adm       2715648 Dec  3 13:51 /var/log/mail.log

Datasource: IBM AIX Version 3 Commands Reference Volume 1
A bit dated but it works just fine on Debian 7.

Reply

43 Deepak March 13, 2014 at 10:33 am

How to find the largest top 10 files with the file modified date?

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: