Find Large Files in Linux

Introduction

Over time, your hard drive may become clogged with a huge number of unneeded files that take up a lot of disc space. Large log or backup files frequently cause Linux systems to run out of disc space.

In this tutorial, we will explain how to use the find and du commands to locate the largest files and directories in Linux systems. We will also address a few FAQs on Finding Large Files in Linux.

Find Large Files Using the find Command

One of the most powerful tools in the Linux system administrator's arsenal is the find command. It lets you look for files and directories using a variety of parameters, including file size.

For example, in the current working directory, you would use the following command to look for files larger than 100 MB:

sudo find . -xdev -type f -size +100M
💡
Replace . with the path to the directory where the largest files should be found.

The output will consist of a list of files with no further information.

Output

/var/lib/libvirt/images/centos-7-desktop_default.img
/var/lib/libvirt/images/bionic64_default.img
/var/lib/libvirt/images/win10.qcow2
/var/lib/libvirt/images/debian-9_default.img
/var/lib/libvirt/images/ubuntu-18-04-desktop_default.img
/var/lib/libvirt/images/centos-7_default.img

The find command can also be used in conjunction with other programs to execute operations on those files, such as ls or sort.

The output of the find command is piped to the ls command, which prints the size of each detected file, and then to the sort command, which sorts it based on the 5th column, which is the file size.

find . -xdev -type f -size +100M -print | xargs ls -lh | sort -k5,5 -h -r

You will get an output like below:

Output

-rw-------  1 root   root 40967M Jan  5 14:12 /var/lib/libvirt/images/win10.qcow2
-rw-------  1 root   root  3725M Jan  7 22:12 /var/lib/libvirt/images/debian-9_default.img
-rw-------  1 root   root  1524M Dec 30 07:46 /var/lib/libvirt/images/centos-7-desktop_default.img
-rw-------  1 root   root   999M Jan  5 14:43 /var/lib/libvirt/images/ubuntu-18-04-desktop_default.img
-rw-------  1 root   root   562M Dec 31 07:38 /var/lib/libvirt/images/centos-7_default.img
-rw-------  1 root   root   378M Jan  7 22:26 /var/lib/libvirt/images/bionic64_default.img

If the output contains many lines of data, the head command can be used to print only the top 10 lines:

find . -xdev -type f -size +100M -print | xargs ls -lh | sort -k5,5 -h -r | head

Let's discuss the command in detail:

  • find. -xdev -type f -size +100M -print - search only files (-type f) in the current working directory (.), greater than 100MB (-size +100M), don't descend directories on other file systems (-xdev), and print the whole file name on the standard output, followed by a new line (-print).
  • xargs ls -lh - the output of the find command is piped to xargs, which runs the ls -lh command, which prints the output in a human-readable long listing format.
  • sort -k5,5 -h -r - sort lines by the fifth column (-k5,5), compare values in human-readable format (-h), then reverse the result (-r).
  • head: only prints the top ten lines of piped output.

The find command has a lot of useful features. You can, for example, look for huge files older than x days, large files with a given extension, or large files belonging to a specific user.

Find Large Files and Directories with du Command

The du command is used to estimate file space consumption and is especially useful for locating directories and files that take up a lot of space on your hard drive.

The following command will print the files and folders with the largest sizes:

du -ahx . | sort -rh | head -5

The file size is listed in the first column, while the file name is listed in the second:

Output

55G	.
24G	./.vagrant.d/boxes
24G	./.vagrant.d
13G	./Projects
5.2G	./.minikube

The command's explanation is as follows:

  • du -ahx .: calculate disc space consumption in the current working directory (.), count both files and directories (a), output sizes in a human-readable format (h), and skip directories on other file systems (x).
  • sort -rh: sort lines by comparing values in a human-readable format (-h), then reverse the result (-r).
  • head -5: only prints the first five lines of piped output.

There are other arguments in the du command that can be used to fine-tune the output of the disc space utilization.

FAQs on Finding Large files in Linux

What is an efficient command for finding large files in Linux? 

One efficient command is: find /path/to/directory -type f -size +100M. This command searches for files larger than 100 megabytes within the specified directory.

Can I find large files in a specific directory and its subdirectories? 

Yes, you can include the -r or -R option within the find command to search recursively through a directory and its subdirectories for large files.

How do I sort the results by file size when finding large files? 

You can use the --sort flag with the ls command. For example, ls -lhS lists files in human-readable format (-h) and sorts them by size (-S) in descending order.

How can I limit the number of results when searching for large files? 

By using the head command in combination with the find command, you can limit the output to a specific number of results. For example: find /path -type f -size +100M | head -n 10 shows the top 10 largest files.

Is it possible to search for large files based on the last modification time? 

Yes, you can modify the find command to include a condition based on the file's modification time. For example, to find files larger than 100MB modified within the last 30 days: find /path -type f -size +100M -mtime -30.

How do I delete large files that I find using the find command? 

You can combine the find and rm commands to remove large files. For example: find /path -type f -size +1G -exec rm {} + deletes files larger than 1 gigabyte.

Are there graphical tools available to find large files in Linux? 

Yes, Linux offers various graphical tools like Baobab, Filelight, and GNOME Disk Usage Analyzer that provide a visual representation of disk usage, facilitating the identification of large files.

Conclusion

Using the find and du commands, we've taught you how to locate the largest files and folders.

You might want to read our post on How to Remove Files and Directories Using Linux Command Line, now that you know how to find the largest files on your system.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.