In some situations, the disk drives get cluttered with a lot of unnecessary files; we need to locate the files eating up most space of our Linux systems.
Over time, log and backup files expand and fill in the available storage. Therefore, recognizing how to spot these files will help us make more informed decisions about disk space management and allocation.
If we are constantly running out of space, we may try this straightforward method to localize, with minimal effort, the most extensive files on our Linux machines.
find
commandOne of the handiest commands enables us to find files according to different criteria, including the file size, and perform subsequent operations.
The syntax of the find
command:
Find
[path]
[options]
[expression]
Let's now search for files larger than 20 MB starting from the root directory:
find / -xdev -type f -size +20M
Let's break down the arguments of the find
command given above:
Argument | Description |
/ | This is the root directory where to start our search. |
-xdev | This restricts the search to the current filesystem. Not adding this argument will broaden the search to other mounted filesystems example, an attached USB drive. |
-type f | This limits our search to files only. |
+size 20M | This finds files larger than 20 MB. |
Now, we will go further and try to expand our search further by combining the find command with the ls
and sort
commands to show only the top 5 files larger than 20 MB.
find / -xdev -type f -size +20M | xargs ls -lh | sort -k5 -nr | head -n 5
Let's go over the newly added arguments of the find
command given above:
Argument | Description |
xargs | Reads the output of the |
ls -lh | Provides detailed information about the resulting files in human-readable file format. |
sort -k5 -nr | Sorts the results based on the file size column (fifth column) in reverse order. |
head -n 5 | Prints the top 5 lines of the given input. |