
In Linux, it is very necessary to find the unnecessary files and free up them from your hard disk. Usually, Linux systems run out of disk space due to large log or backup files. This tutorial explains how to find the large files and directories in Linux systems using the find and du command.
Find Large Files Using the find Command
The find command is very useful tools in the Linux systems. Using it you can search for files and directories with specific criteria.
For example, if you want to find files with size greater than 200MB in current working directory, type:
sudo find . -xdev -type f -size +200M
You should replace . dot with the path if you want to find in specific directory.
The above command will show a list of files without any additional information.
/home/tecnstuff/bkp.zip /home/tecnstuff/image1.jpg
You also can use find command with combination of other commands such as ls or sort to perform operations on those files.
For example, we will pass the output of the find command to ls which will show the size of the each found file.
find . -xdev -type f -size +100M -print | xargs ls -lhIt will show the below output:
-rw------- 1 tecnstuff tecnstuff 365M Dec 28 01:10 /home/tecnstuff/bkp.zip -rw------- 1 tecnstuff tecnstuff 290M Mar 7 20:06 /home/tecnstuff/image1.jpg
If there are more lines in output you can combine head command to show only first 10 line as given below:
find . -xdev -type f -size +100M -print | xargs ls -lh | head
Following is the explaination of the command:
- find . -xdev -type f -size +100M -print– It will search only for files (- -type f) in the current working directory, file size larger than 200MB, 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- xargswhich executes the- ls -lhcommand that will print the output in long listing human-readable format.
- head: To prints only the first 10 lines of the piped output.
The find command has lot of powerful options. For example, you can search for large files that are older than specific 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
Generally, the du command is used to finding directories and files that consume large amounts of disk space.
The below command will display the list of largest files and directories:
du -ahx . | sort -rh | head -5
35G . 24G ./images 12G ./data
Let’s break down the command:
- du -ahx .: It estimates disk space usage in the current working directory,- awill print count both files and directories ,- hprints sizes in a human-readable format and- xskips directories on different file systems.
- sort -rh: It will sort lines by comparing values in human-readable format (- -h) and reverse the result (- -r).
- head -5: prints only the first 5 lines of the piped output.
The du command includes more other options.
Conclusion
It is necessary to find large files and free it up, when your disk is full and getting out of space.
If you have any questions or remarks, please leave a comment below.
