There are times when our system is running out of disk space. We may need to find these files to delete them or even archive them in some scenarios. There will obviously be critical files that we may want to retain. The action we take on these files will depend on the types of files we find.
Just like the largest files, we may also want to find the largest directories. It could be possible that the
/tmp directory has gigabytes of temporary files and maybe eating space on the hard drive. Sometimes it is easy to target the directory rather than the individual file. We can use various commands to find the large files in Linux.
In this tutorial, we will explain how to find large files in Linux using the
Another useful command that can help when searching for large files is the du command.
Using the find Command
In Linux, we can use the
find command to find files and directories. For example, we can search for empty directories with the
Similarly, we can use the
find command to find files with more than a specific size.
Let’s take an example of finding files that are more than 50 MiB in size in the /var directory.
To do this, we’ll execute the following command:
sudo find /var -size +50M -ls
Let’s explain the above command.
We need to define the directory path, which is done with the /var path. Then, we need to use the
-size option to define the size, which can be defined with
+ (more than) or
- (less than). In this scenario, we use the + sign, which means that the find the command will find the files with more than 50 MiB in size.
In the end, the
ls command will list the output. If we omit the ls parameter, the files will be listed, but the file size will not be displayed.
40383 56752 -rw------- 2 root root 58114048 Jun 4 2021 /var/lib/snapd/snaps/core18_2066.snap 40384 69188 -rw------- 2 root root 70848512 Jun 4 2021 /var/lib/snapd/snaps/lxd_20326.snap 40383 56752 -rw------- 2 root root 58114048 Jun 4 2021 /var/lib/snapd/seed/snaps/core18_2066.snap 40384 69188 -rw------- 2 root root 70848512 Jun 4 2021 /var/lib/snapd/seed/snaps/lxd_20326.snap
Let’s try the same command without the ls parameter:
find /var -size +50M
The output does not list the file sizes.
/var/lib/snapd/snaps/core18_2066.snap /var/lib/snapd/snaps/lxd_20326.snap /var/lib/snapd/seed/snaps/core18_2066.snap /var/lib/snapd/seed/snaps/lxd_20326.snap
Let’s give a size range and find files between that range in the /var directory. To do this, we need to execute the following command:
find /var -size +50M -size -60M -ls
Previously, we had used only the
+ (more than) option. Now, we are specifying the
- both. This creates a size range more than 50 MiB and less than 60 MiB. All files within this range will be tracked.
40383 56752 -rw------- 2 root root 58114048 Jun 4 2021 /var/lib/snapd/snaps/core18_2066.snap 40383 56752 -rw------- 2 root root 58114048 Jun 4 2021 /var/lib/snapd/seed/snaps/core18_2066.snap
We can also use the find command to locate files of a specific extension.
For example, we want to find the 10 largest files with the .sh extension and print the list descending on the screen. Rather than listing all the files, we want to limit the results to only 10 files.
We will be locating the files in the /etc directory. We can consider the directory name as a variable because we can use any other directory, such as /var. Another important part of this command is the -type parameter, which has the value
f. This means that it will list only the files and skip listing directories.
sudo find /etc/ -type f -iname "*.sh" -printf '%s %p\n'| sort -nr | head -10
Notice that the output lists only 10 files with the
.sh extension in the /etc/ directory.
3809 /etc/init.d/hwclock.sh 3417 /etc/profile.d/Z99-cloud-locale-test.sh 1557 /etc/profile.d/Z97-byobu.sh 1479 /etc/init.d/keyboard-setup.sh 1232 /etc/init.d/console-setup.sh 1003 /etc/profile.d/cedilla-portuguese.sh 898 /etc/profile.d/update-motd.sh 873 /etc/profile.d/Z99-cloudinit-warnings.sh 833 /etc/profile.d/apps-bin-path.sh 757 /etc/profile.d/gawk.sh
For example, if we type the following command:
sudo find /etc/ -printf '%s %p\n'| sort -nr | head -10
The output that we get is completely different. So, what is the difference between this command and the previous one? In this command, we did not add the following parameters:
-type f -iname "*.sh"
The find command looks for the files and directories without these two parameters,
535195 /etc/ssh/moduli 200313 /etc/ssl/certs/ca-certificates.crt 102257 /etc/lvm/lvm.conf 86435 /etc/vmware-tools/vgauth/schemas/XMLSchema.xsd 58549 /etc/udev/rules.d/70-snap.snapd.rules 28313 /etc/ld.so.cache 26703 /etc/apparmor.d/usr.lib.snapd.snap-confine.real 24546 /etc/mime.types 17394 /etc/X11/rgb.txt 16384 /etc/ssl/certs
Well done. Hopefully, this tutorial helped you use
find command to find large files in Linux. If you encounter any issues, please feel free to leave a comment or contact us, and we’ll get back to we as soon as we can.