The command
If you want to find the largest folders within your Linux system, see this post instead. Here is the command to find the top 20 largest files in a given directory and print their size in a human readable format (eg. 2.4M, 24G).
# the full command find path/to/your/directory -type f -exec du -h {} + | sort -hr | head -20
Examples
Find the 20 heaviest files in my /Documents directory and print their human readable size
# replace /home/naysan/Documents/ with the directory you want to scan find /home/naysan/Documents/ -type f -exec du -h {} + | sort -hr | head -20
Find the top 10 heaviest files in your entire system and print their human readable size. You can narrow the find to files of at least 100 Megabits. Also since you will be searching all your system you want to execute this command as sudo.
# This might take a long time to execute, because you are scanning your entire system sudo find / -type f -size +100M -exec du -ah {} + | sort -hr | head -10
Step-by-step breakdown
You can try each of the following commands for yourself to see the results. Just make sure you replace /home/naysan/Documents with the path to a directory of your choice in your system.
1- find
# find all files within a directory find /home/naysan/Documents/ -type f
This command simply finds all the files (not directories) within your directory.
2 – exec du -h {} +
# find all files, then get their human readable size find /home/naysan/Documents/ -type f -exec du -h {} +
Now for each file found, the –exec command will execute the command du -h, which print every file size in human readable (eg 2.4M). For more information
- the {} asks the du command to take the results of the find command as input
- the + sign asks the du command to treat all the files at once (which makes it a bit faster)
3 – | sort -hr
# find all files, then get their human readable size, then sort by largest first find /home/naysan/Documents/ -type f -exec du -h {} + | sort -hr
The | symbol is a Linux pipe, which redirects the output of the last command (up until du -h {} +) to the new command, which is sort. So you are now essentially sorting all files found (with their human-readable size) by human readable size (-h), in reverse order (-r).
3 – | head -20
# find all files, then get their human readable size, then sort by largest first, then only print the top 20 find /home/naysan/Documents/ -type f -exec du -h {} + | sort -hr | head -20
Again, you use the | symbol to take the output of the last command (sorted files + size) and use that as an input to the head command, which prints the top 20 files here. Head -5 would print the top 5 files etc.
That’s it! 🙂