Find the largest files on your server
August 7th, 2007Sometime it is important to see which files or directories are taking up all the disk space on your server. You may want to check the entire server or a specific directory and subdirectories.
For example, my server with several websites on it suddenly redlined in disk usage. I have a 65gb disk that suddenly became 98% full within a few weeks, threatening to crash my server. I had to find which file(s) were the culprit.
There is no simple command available to find out the largest files or directories on a Linux/UNIX/BSD filesystem. However, the combination of the following three commands (using pipes) you can easily find out list of largest files:
du : Estimate file space usage
sort : Sort lines of text files or given input data
head : Output the first part of files i.e. to display first 10 largest file
Here is what you need to type at shell prompt to find out top 10 largest files or directories is taking up the most space in your file system:
# du -a | sort -n -r | head -n 10
If you want to check a specific directory, such as /home, just use this…
# du -a /home | sort -n -r | head -n 10
If you want a more human readable output use:
# du -ks /home | sort -n -r | head -n 10
Nomenclature as follows:
-a : Include all files, not just directories (du command)
-h : Human readable format
-n : Numeric sort (sort command)
-r : Reverse the result of comparisons (sort command)
-n 10 : Display 10 largest file. If you want 20 largest file replace 10 with 20.
Using these commands, I was able to locate a 20gb error_log file in a location where I had previously run a php script that went awry. I would have never guessed to look in that location. So this know-how saved me a TON of frustration, guess work, and ultimately my server from crashing.


























