In this guide we a going to check how to find largest files in linux. If you have a linux server hosting applications and the server is running out of space, you will probably be asking yourself how you would go about finding what is eating up all that space.
The Linux commands find and du will come to your rescue.
Using the du command
The du command is used to estimate file space usage on Linux system. It shows the disk usage information.
Lets use du to check the content of /boot:
| |
The values on the far left are the disk usage, followed by the specific directory responsible for that usage. The bottom row is a summary of the entire /boot/ directory.
Here is a list of important du options
-h,--human-readableprints size outputs in a human-readable format.-s,--summarizecan be combinend with-hget a summary of the directory’s usage in a human-readable format.-a,--alllists the sizes of all files and directories in the given file path. You can combine with-h
Now in our case, we would want to file space usage and to check the largest first, we can sort them using the sort command. If we want to limit our result, the head command will come in handy,
Getting the largest files with du:
| |
The above command uses du to get disk usage, then the content is piped to sort, then head will only output the first 20 items.
Output:
| |
Often times you will get some errors before you get your list of large files. This often comes from either files that you are not allowed to access or any stderr output. Use 2>/dev/null to ignore those like in this command:
| |
Using the find command
You can use the find command to target only files in a search and find the size of each, then use a combination of sort and head to filter out the content.
Example:
| |
The above command searches for all files in the system, then prints the size and path using the %s and %p directives . The result is then piped to sort to filter from the largest to the smallest then the head will limit to 10 result. The -n is for numeric sort and the -r passed to sort will reverse the result of comparisons.
Output:
| |
Conclusion
From the above explanation, you learnt how to get the largest and biggest files and directories in Linux. We also learnt how to use the sort command to sort the returned output and the head command to only limit the result to the number we specified.
To check more on the commands we used, don’t hesitate to use the man pages. Use these commands:
man du
man find
man sort
man head
man tail