How Find Large Files With Du And Find Command.
This tutorial explains how to find the largest files and directories in Linux systems using the find and du commands.
Find Large Files Using the find Command.
. 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.
The find command also can be used in combination with other tools such as ls or sort to perform operations on those files.
In the example below, 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.
The output will look something like this:
If the output contains a lot of lines of information you can use the head command to print only the first 10 lines:
Let’s break down the command:
find . -xdev -type f -size +100M -print – search only 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 – the output of the find command is piped to xargs which executes the ls -lh command 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 the values in human-readable format (-h) and reverse the result (-r).
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
The 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:
The first column includes the size of the file and the second one the file name:
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 (x).
sort -rh : sort lines by comparing values in human-readable format (-h) and reverse the result (-r).
head -5 : prints only the first five lines of the piped output.
The du command has many other options that can be used to refine the output of the disk space usage.
We’ve shown you how to find the largest files and directories using the find and du commands.