Find large files in multiple directories

I haven’t posted for awhile because I have been super busy. I don’t think I have had a chance to post anything since it was announced that the company I work for was being bought out in the beginning of May. That sale has now gone through earlier this week, but the workload has not decreased in the slightest.

Anyway, most admins know how to find the largest files inside a single directory:

du -sk * | sort -n

The other day I needed to find the largest of over 10k+ files spread across hundreds of directories. So with a little command combo magic, I used this:

for i in `find . -type f`; do du -sk $i; done | sort -n

Which runs a du -sk on every single file within the current directory and all sub-directories, pipes it to a sort, with the last files being output to the screen being the largest.

Looking at it again today, I guess I probably could have removed the for loop and just done:

du -sk `find . -type f` | sort -n

Testing it out real quick shows that it works; however, if you have as many files as I had to go through, you would have quickly exhausted du and it would have complained:

-bash: /usr/bin/du: Argument list too long

So it looks like the for loop was needed after all. Personally, I love running for loops on the command line.

Happy Father’s Day to all you fathers out there!





Please VOTE for this page at: ADD TO DEL.ICIO.US | ADD TO DIGG | ADD TO FURL | ADD TO NEWSVINE | ADD TO NETSCAPE | ADD TO REDDIT | ADD TO STUMBLEUPON | ADD TO TECHNORATI FAVORITES | ADD TO SQUIDOO | ADD TO WINDOWS LIVE | ADD TO YAHOO MYWEB | ADD TO ASK | ADD TO GOOGLE


3 Comments


  1. This takes a while to run, but try

    du -mxS / | sort -n | tail -n 40

    I can’t remember where I picked that up, or what each of the options are, but it gives your the 40 largest directories on the system.

    Posted June 16, 2008, 11:40 am

  2. Xargs will solve the “Argument list to long” problem, info is here they can explain it better then I can. http://en.wikipedia.org/wiki/Xargs

    Posted June 24, 2008, 2:49 pm

  3. And for files with spaces in the middle of names on solaris (top 20 files):

    find . -type f | while read filename; do du -sk "$filename"; done | sort -n | tail -20

    Posted January 19, 2010, 2:27 pm

Leave a reply