You are hereTip: Largest Files in Linux

Tip: Largest Files in Linux


By steve - Posted on 24 February 2012

Here's a quick tip on how to find the largest files on your Linux system. If you are running low on disk space, knowing your largest files can be very handy for getting rid of especially large unneeded files and getting things going, again. I know I sometimes set aside a backup of a database that might be 20GB or more, such as during a migration or upgrade or debug session, then end up forgetting I have it sitting there. When space gets tight, I look for the largest files first, then I don't have to spend a lot of time getting things back where they belong.

This command will list the 30 largest files on your system and sort from largest file size down in size. This 2015 version is improved and takes into account filenames with spaces or escaped characters:




find / -mount -type f -printf '%s|"%h/%f"\n' | \
 awk '{FS="|"; printf "%10d MB\t%s\n",($1/1024)/1024,$2}' | \
 sort -rnk1 | head -30

Run it as all one line.

If you want to list more than 30 files, change the -30 to whatever you want, like -100 (it won't take any longer, all of the heavy lifting happens before this step). If you want to use this command in cygwin, just replace the / with /cygdrive/c (using c for drive c, d for drive d, etc.).

If you run it from the root folder and you have a large file system, this can take a bit to run. On a 8GB memory/1TB SSD (80% full) Windows 7 system with cygwin installed, it takes about 5 minutes to show the 30 largest files. A 4GB memory/160GB disk (88% full) Ubuntu server on Rackspace took 15 seconds.

This is how it works:
First, we get all of the files (no directories) in the specified file system and output the size in bytes, a pipe symbol for the delimiter, then the quoted directory and file name:

find / -mount -type f -printf '%s|"%h/%f"\n'

That output is then formatted to a fixed column number of MBs and the directory/filename in quotes (splitting fields at the pipe symbol):

awk '{FS="|"; printf "%10d MB\t%s\n",($1/1024)/1024,$2}'

We sort that list in reverse (largest to smallest) as a number starting at the first character of the data:

sort -rnk1

Finally, we limit the list to the first 30 answers:

head -30

My previous short command (deprecated) that didn't always tell you the entire file name and path because of spaces was this:

find / -mount -type f -ls|sort -rnk7 |head -30| 
   awk '{printf "%10d MB\t%s\n",($7/1024)/1024,$NF}'

Run it as all one line.

Did this help you? You can help me!


Did you find this information helpful? You can help me back by linking to this page, purchasing from my sponsors, or posting a comment!


+One me on Google:


Follow me on twitter: http://twitter.com/mojocode







Comments



Affiliation Badges