A BASH Script to Find Large Files on a Linux Server
I don’t have Plesk or any other control panel set up on my server, and I don’t have disk or user quotas setup because I like to manage my hosting myself, and I don’t provide hosting to a vast number of clients. So when I recently had a situation where I had to find a bunch of files taking up a lot of space on my server I talked to my hosting company, Rackspace, and they showed me a great script to look for files over a certain size on my server. I wanted to share it with all of you.
The script essentially goes through your file system using the find command and grabs any file over the specified file size.
First, create a new text file and paste this code into it:
#!/bin/bash
# if nothing is passed to the script, show usage and exit
[[ -n "$1" ]] || { echo “Usage: findlarge [PATHNAME]“; exit 0 ; }
# simple using find, $1 is the first variable passed to the script
find $1 -type f -size +100000k -exec ls -lh {} \; | awk ‘{ print $9 “: ” $5 }’
Save the file (ex: findlarge.sh) and then make it executable with the following command:
chmod a+x findlarge.sh
Now run the script and send any output to a new file (largefiles.txt):
./findlarge.sh / > largefiles.txt &
This will run the findlarge.sh script and passes in the location to start searching (in this case the root directory) and then uses the ‘>’ to redirect all output to a file named largefiles.txt. Finally, because the script may take a while, I add an ampersand to the end so the script is run in the background. Also note: you may have to run this as root if you want to run this on the root of the file system since you will need permission to read all the files and access all directories on the server if you want to scan the entire file system. If you want to only scan the users home directories then you can run ./findlarge.sh /home > largefiles.txt & or any other specific directory.
Currently, this script will find any file over 100,000 kilobytes (100MB). If you want to change it to find files over 200MB then just change the +100000k to +200000k, or some other file size.
Anyway, I hope this helps, it has helped me a lot.




February 21st, 2008 at 11:26 am
Thanks for the script, it was exactly what I was looking for, but the last line won’t work with out a before the ;
The line should read:
find $1 -type f -size +100000k -exec ls -lh {} ; | awk ‘{ print $9 “: ” $5 }’
February 21st, 2008 at 11:30 am
Well now that’s goofy the backslash didn’t display, so I guess you did give us the correct script. It must be that what ever software you are using to power your website can’t parse/display the backslash correctly.
Anyway, the line should read
find $1 -type f -size +100000k -exec ls -lh {} backslash; | awk ‘{ print $9 “: ” $5 }’ where backslash has been substituted by the actual character.
February 21st, 2008 at 11:36 am
Good find! I’ve fixed it in the post. Thanks!
February 22nd, 2008 at 3:20 pm
[...] This prompted me to google a bash script that would search the file system for files bigger than a given value, and since google never does me wrong, I came across this site. [...]
May 16th, 2008 at 11:28 am
Thank you very much. This script was exactly what I needed also. You’ve saved me a ton of time in managing my server. Again, thanks.
May 24th, 2008 at 12:35 am
[...] A BASH Script to Find Large Files on a Linux Server [...]
November 5th, 2008 at 10:47 pm
Any know why this does not work on the mac OSx?
Thank you,
FdeMelo
January 8th, 2009 at 6:18 am
hi, nice script.. i a question.. how can i display the whole path dir? i have some large files which residing to dir that has spaces.. when I run this script the whole path is not showing..
thanks
March 1st, 2009 at 3:34 am
[...] MauricioVieira on Feb.28, 2009, under software development Nice and smart script using find and awk to find large files. :bash unix scripting No comments for this entry [...]