How to find the biggest Directories and Files in Linux
- If you want to find the biggest directories under a partition (For eg, /home), you will need to run the following command.
du -ah /home | sort -n -r | head -n 15
- To display the biggest directories in the current working directory, you will need to run following command.
du -ah | sort -n -r | head -n 15
du – It is used to estimate file space usage.
a – It is used to displays all files and folders.
sort – It is used to sort lines of text files.
-n – It is used to compare according to string numerical value.
-r – It is used to reverse the result of comparisons.
head – It is used to output the first part of files.
-n – It is used to print the first ‘n’ lines. (In this case, the command displayed first 15 lines).
- To display the largest folders/files including the sub-directories, you will need to run following command.
du -Sh | sort -rh | head -15
du – It is used to estimate file space usage.
-h – It is used to print sizes in human readable format (e.g., 10 MB).
-S – It is used to exclude size of subdirecis used tories.
-s – It is used to display only a is used total for each argument.
sort – It is used to sort lines of text files.
-r – It is used to reverse the result of comparisons.
head – It is used to output the first part of files.
- To display the biggest file sizes only, you will need to run the following command:
find -type f -exec du -Sh {} + | sort -rh | head -n 15
- To find the biggest files in a particular location, you will need to just include the path besides the find command:
find /home/username/Downloads/ -type f -exec du -Sh {} + | sort -rh | head -n 15
OR
find /home/username/Downloads/ -type f -printf "%s %p\n" | sort -rn | head -n 15
Note :-
- The above command will display the largest file from directory /home/username/Downloads/
- Replace /home/username/Downloads/ as per your requirement.