DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Find All Large Files On A Linux Machine

02.15.2006
| 289599 views |
  • submit to reddit
        Finds all files over 20,000KB (roughly 20MB) in size and presents their names and size in a human readable format:

find / -type f -size +20000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }' 
    

Comments

Snippets Manager replied on Thu, 2010/06/17 - 3:34am

why so complicated? find . -size +20000k -exec du -h {} \; that's good enough for me.

Kevin White replied on Mon, 2011/04/04 - 6:26pm

@michaelshiloh Your sort should use the '-h' option instead of the '-n' to make it sort by human-readable numeric, i.e. 1M 20G etc. When I used your snippet, it sorted the 2.1GB file at the very bottom (even though it was the largest of all the files in the list) because 'sort -n' only saw the 2.1 and not the GB find / -type f -size +20000k -exec ls -lh {} \; 2> /dev/null | awk '{ print $NF ": " $5 }' | sort -hrk 2,2

Snippets Manager replied on Fri, 2009/04/17 - 12:17pm

I forgot to add to sort in reverse order, so you get the biggest ones first: find / -type f -size +20000k -exec ls -lh {} \; 2> /dev/null | awk '{ print $NF ": " $5 }' | sort -nrk 2,2 Next step will be to direct find to skip virtual directories like /proc and /sys

Snippets Manager replied on Fri, 2009/04/17 - 12:17pm

Find reports errors when it searches directories for which it has no permissions. To discard these errors, redirect stderr to /dev/null: find / -type f -size +20000k -exec ls -lh {} \; 2> /dev/null | awk '{ print $NF ": " $5 }' Additionally, I find it handy to sort (numerically) based on the size (second column): find / -type f -size +20000k -exec ls -lh {} \; 2> /dev/null | awk '{ print $NF ": " $5 }' | sort -nk 2,2

Snippets Manager replied on Thu, 2009/03/12 - 1:42am

actually it's probably just better to use the NF in place of numbers in that case no? find / -type f -size +20000k -exec ls -lh {} \; | awk '{ print $NF ": " $5 }'

Snippets Manager replied on Tue, 2008/07/22 - 10:50am

It does work as shown on older versions of RedHat, and likely others. The output of awk seems to have changed a bit over the years and on Ubuntu (probably all Debian) based system the above command will only show a list of file sizes. As syncshaker pointed out on Ubuntu systems change the $9 to a $8 and it will work: find / -type f -size +20000k -exec ls -lh {} \; | awk '{ print $8 ": " $5 }'

Snippets Manager replied on Tue, 2008/04/08 - 5:19am

You can customize this command by changing e.g. / by . or any directory path, or - and zoestardust you're right to point it out - by replacing in awk $9 by likely $8. Anyway the command is globally right ;) ( a typo ? )

Snippets Manager replied on Tue, 2008/02/26 - 11:55pm

This does not show names or locations, it just lists a bunch of file sizes