Find the largest file(s) and folder(s) in Linux via SSH

A Linux-based operating system is a powerful machine. However, most webservers are only accessible via SSH and you have to do all the work with the use of the command line. Recently, one of my webservers was using a lot of disk space. To investigate what file or files were taking up all my free space, I had to find the right command to show me the biggest files on my whole server.

Biggest files AND folders

To find a list of your 10 biggest files and directories within your home directory, use this command:

du -a -h /home | sort -n -r | head -n 10

A sample output can be:

# du -a -h /home | sort -n -r | head -n 10
1020K /home/log/example.php
1016K /home/www/html/encoding
1012K /home/log/wtmp
1008K /home/lib/mysql/example_sql
1000K /home/www/html/roundcubemail-1.0.2/plugins/jqueryui
948K /home/log/messages-20150524
944K /home/www/icons
940K /home/www/html/img
932K /home/www/html/advanced
916K /home/lib/mysql/example.MYD

You can change the parameters. You can also look for the largest files on the whole system by changing /home to / or change the output from 10 to 20 files by changing -n 10 to -n 20. This could look like:

du -a -h / | sort -n -r | head -n 20

Biggest files only, NO folders

So the above two examples will give you both the biggest files and directories. If you only want to find the biggest files and not the biggest directory, the find command can help you.

find /home -type f -printf '%s %p\n'| sort -nr | head -10
# find /home -type f -printf '%s %p\n'| sort -nr | head -10
2654K /home/log/example.php
445K /home/www/html/test.html
270K /home/log/wtmp/temp.txt
268K /home/lib/mysql/example_sql.sql
111K /home/lib/mysql/example.MYD

Find the biggest directories only, NO files

If you do not want to find the biggest files, but only want to have a list of the directories that take up the most disk space, use this command:

du -k /home | sort -n | tail -10
# find /home -type f -printf '%s %p\n'| sort -nr | head -5
999445K /home/folder1
23245K /home/movies
99234K /home/log/wtmp/
96444K /home/lib/mysql/biggest-databases
77340K /home/example

 

Author Bio

Thank you for your interest in my blog! On this miniblog, I write mostly short (technical) blog posts that might interest other people. Read more about me or feel free to contact me.

 

Leave a Reply

Your email address will not be published. Required fields are marked *