Find Largest Files & Directories On Linux
As part of our server management obligations we frequently need to track down the cause of excessive disk usage, which can be anything from out of control log files to poorly cleaned up import/export files. To quickly and efficiently find largest files the following commands come in very useful. All rely only on default system tools that should be available on any Linux system but for reference we generally favour Ubuntu LTS so that’s where most of these snippets have been tested.
First off a very handy search to help find largest files on the system. In this example we look for the top 10 files by file size, anywhere in or below the current directory.
$ sudo find -type f -exec du -Sh {} + | sort -rh | head -n10 2.9G ./var/lib/mysql/[***]/[***].ibd 2.4G ./var/lib/mysql/[***]/[***].ibd 354M ./var/www/[***]/[***]/[***]/[***]/[***].log 333M ./var/lib/mysql/[***] 206M ./var/www/[***]/[***].log 178M ./var/www/[***]/[***]/[***]/[***]/[***].sql 173M ./var/lib/mysql/[***]/[***].ibd 140M ./var/lib/mysql/[***] 113M ./var/lib/mysql/[***]/[***].ibd 102M ./var/www/[***]/[***]/[***]/[***]/[***].log
This command runs with root privileges so it can scan files belonging to all system users, if you only need to see files you have permission to read you can drop the sudo
prefix. The command is in three parts, first all files are listed, then they are sorted by size, then only the top 10 lines are shown. By changing the -n10
value you can view different numbers of files.
If we want to find largest files in an alternative directory we can add it to the immediate right of the find
command, as in the following example. Notice we also changed the number of files to list.
$ sudo find /var/lib/mysql -type f -exec du -Sh {} + | sort -rh | head -n5 2.9G ./var/lib/mysql/[***]/[***].ibd 2.4G ./var/lib/mysql/[***]/[***].ibd 333M ./var/lib/mysql/[***] 173M ./var/lib/mysql/[***]/[***].ibd 140M ./var/lib/mysql/[***]
Worth mentioning here this is also a useful directory to investigate if you’re looking for the largest database tables on the system. Of course /var/lib/mysql
is only the default MySQL data directory, so if it’s been changed you’ll have to adjust the command accordingly.
If instead we want to find the largest directories the following command is useful. This might be the case if we want to track down any directories which contain a large number of small files. These would be ‘hidden’ with the above command, but revealed when we look at directory sizes as a whole.
$ du -h | sort -rh | head -n5 20G pub/[***] 20G pub 18G pub/[***]/[***] 13G pub/[***]/[***]/[***] 9.9G pub/[***]/[***]/[***]/[***]
The above shows the top 5 directories within the current directory, sorted by total size. If you want to query another directory, just specify it to the right of the du
command.
$ du -h pub/ | sort -rh | head -n5 20G pub/[***] 18G pub/[***]/[***] 13G pub/[***]/[***]/[***] 9.9G pub/[***]/[***]/[***]/[***] 5.5G pub/[***]/[***]/[***]
Thanks to how MySQL stores it’s data, if we use the above command but instead specify the MySQL data directory we can get a quick idea of how large each database on the system is.
$ du -h /var/lib/mysql/ | sort -rh | head -n5 6.0G /var/lib/mysql/ 4.8G /var/lib/mysql/[***] 517M /var/lib/mysql/[***] 59M /var/lib/mysql/[***] 26M /var/lib/mysql/[***]
We’ll be featuring more useful snippets like this in the future, so check back for more or get in touch if there’s anything in particular you’d like covering.