Finding large files in Linux

In today’s article, we are going to show you how to find large files in Linux. One of the most common manipulations that you will do as a Linux system administrator is to find unnecessary large files that take up disk space and delete them to make room for applications that need extra space. Let’s dive deeper and find out how we can find large files in Linux.

1. Detecting large directories and files in Linux

First, we’ll take a look at how we can find the largest files and directories on Linux by running the following command to find the top 10 largest directories and files on a Linux server:

# du -ah /* 2>/dev/null | sort -rh | head -n 10

The output should show you the largest directories and files:

4.3G    /backup
1.2G    /usr
1.2G    /var
845M    /home
698M    /var/lib
675M    /odoo
634M    /usr/lib
621M    /odoo/odoo-server
622M    /home/largefile3
398M    /odoo/odoo-server/addons

2. Finding the top largest directories in Linux

This command can be very useful when you need to check the size of directories in the root partition to get an idea of ​​how the server space is being used, with the following command you can find the top 10 largest directories in the root partition:

# du -sh /*/ 2>/dev/null | sort -rh | head -n 10

The output should show you the largest directories:

4.3G    /backup
1.2G    /usr/
1.2G    /var/
845M    /home/
675M    /odoo/
143M    /opt/
51M     /lib/
39M     /run/
29M     /root/
9M     /bin/

Alternatively, you can use the following command to check the size of the subdirectories of a given directory, in this case we will use the “var” directory:

# find /var/* -type d -exec du -sh {} 2>/dev/null + | sort -rh | head -n 10

The output should list the subdirectories of the “var” directory:

723M    /var/lib
295M    /var/cache
38M     /var/www
2.8M    /var/log
87K    /var/spool
48K     /var/backups
19K     /var/mail
9K     /var/tmp
4.0K    /var/opt
4.0K    /var/local

3. Finding the top largest files in Linux

Sometimes you can search for large files, especially large log files that can fill your server quite quickly, with the following command you can find the top 10 largest files on the server:

# find / -type f -exec du -sh {} 2>/dev/null + | sort -rh | head -n 10

The output should show you the largest files:

487M    /home/largefile3
345M    /home/largefile2
211M    /home/largefile1
123M    /odoo/odoo-server/.git/objects/pack/pack-dcf71f4c259beb1fgt56е3b4a00a45e1f2a8382.pack
123M    /backup/odoo/odoo-server/.git/objects/pack/pack-dcвdd1f4c259beb35445666аb4a00a45e1f2a8382.pack
98M    /var/lib/mongodb/journal/WiredTigerPreplog.0000000002
197M    /var/lib/mongodb/journal/WiredTigerPreplog.0000000001
100M    /backup/var/lib/mongodb/journal/WiredTigerPreplog.0000000002
100M    /backup/var/lib/mongodb/journal/WiredTigerPreplog.0000000001
66M     /var/lib/mysql/ibdata1

Finding the largest files with a specific extension in Linux

If you are looking for large files with a specific extension, you can find the top 10 largest files by their extension with the following command, we will use the “deb” extension in this case:

# find / -type f -iname "*.deb" -exec du -sh {} + | sort -rh | head -10

The output should show you the largest files containing the specified extension:

30M     /var/cache/apt/archives/mongodb-org-tools_3.2.14_amd64.deb
30M     /backup/var/cache/apt/archives/mongodb-org-tools_3.2.14_amd64.deb
26M     /var/cache/apt/archives/libpython2.7-dev_2.7.12-1ubuntu0~16.04.1_amd64.deb
26M     /backup/var/cache/apt/archives/libpython2.7-dev_2.7.12-1ubuntu0~16.04.1_amd64.deb
13M     /opt/wkhtmltox-0.12.1_linux-trusty-amd64.deb
13M     /backup/opt/wkhtmltox-0.12.1_linux-trusty-amd64.deb
9.5M    /var/cache/apt/archives/mongodb-org-server_3.2.14_amd64.deb
9.5M    /backup/var/cache/apt/archives/mongodb-org-server_3.2.14_amd64.deb
7.9M    /var/cache/apt/archives/g++-5_5.4.0-6ubuntu1~16.04.4_amd64.deb
7.9M    /backup/var/cache/apt/archives/g++-5_5.4.0-6ubuntu1~16.04.4_amd64.deb

Sidebar