Read in 4 minutes
Find Large Files in Linux
Over the period of time, your disk drive may get cluttered with a lot of unnecessary files taking up large amounts of disk space. Usually, Linux boxes run out of disk space due to large log or backup files.
This tutorial explains how to find the largest files and directories in Linux systems using the
Find Large Files Using the find Command
The find command is one of the most powerful tools in the Linux system administrators arsenal. It allows you to search for files and directories based on different criteria including the file size.
For example, if want to search for files with size greater than 100MB, in the current working directory you can use the following command:
sudo find . -xdev -type f -size +100M
.with the path to the directory where you want to search for the largest files.
The output will show a list of files without any additional information.
/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 also can be used in combination with other tools such as
sort perform operations on those files.
Here we are passing the output of the
find command to
ls which will print the size of each found file and then pipe that output to the
sort command to sort 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
The output will look something like this:
-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 a lot of lines of information you can use the
head command to print only the first 10 lines:
find . -xdev -type f -size +100M -print | xargs ls -lh | sort -k5,5 -h -r | head
Let’s break down the command:
find . -xdev -type f -size +100M -print: search for files (
-type f) in the current working directory (
.), larger than than 100MB (
-size +100M), don’t descend directories on other filesystems (
-xdev) and print the full file name on the standard output, followed by a new line (-print).
xargs ls -lh: use
xargsto execute the
ls -lhcommand that will print the output in long listing human-readable format.
sort -k5,5 -h -r: sort lines based on the 5th column (
-k5,5), compare human-readable format (
-h) and reverse the result (
head: prints only the first 10 lines of the piped output.
The find command comes with a lot of powerful options. For example, you can search for large files that are older than x days, large files with a specific extension or large files that belong to a particular user.
Find Large Files and Directories Using the du Command
du command is used to estimate file space usage and it is particularly useful for finding directories and files that consume large amounts of disk space.
The following command will print the largest files and directories:
du -ahx . | sort -rh | head -5
55G . 24G ./.vagrant.d/boxes 24G ./.vagrant.d 13G ./Projects 5.2G ./.minikube
Explanation of the command:
du -ahx .: estimate disk space usage in the current working directory (
.), count both files and directories (
a), print sizes in a human-readable format (
h) and skip directories on different file systems (
sort -rh: sort lines by comparing human-readable format (
-h) and reverse the result (
head -5: prints only the first 5 lines of the piped output.
du command includes other options that you can use to refine the output of the disk space usage.
Now that you have found the largest files on your system you may want to read our guide about How to Remove Files and Directories Using Linux Command Line.
If you have any question or remark, please leave a comment below.