Table of Contents
Ubuntu - Directory - Find largest and smallest directories
Find Largest Directories And Files
To find out the top largest ten directories and files in the current working directory, just run:
sudo du -a | sort -n -r | head -n 10
Where,
- du : Disk usage command that estimates file space usage
- -a : Displays all directories and files
- sort : Sort lines of text files
- -n : Compare according to string numerical value
- -r : Reverse the result of comparisons
- head : Output the first part of files
- -n 10 : Print the first 10
To view the above result in human-readable format (in Kb, MB, GB etc), just add the parameter “h” as shown below.
sudo du -ah | sort -n -r | head -n 10
As you see in the above results, we have listed all files and directories and its sub-directories in the current working directory.
To display the largest directories and files of a particular directory, for example /var, run:
sudo du /var -a | sort -n -r | head -n 10
To display the above results in human-readable format, add “-h” parameter:
sudo du -ah /var | sort -n -r | head -n 10
Let us find out the largest files in the current working directory and its sub-directories:
sudo find -printf '%s %p\n'| sort -nr | head -10
You can skip the directories and display only the files by adding “-type f” flag in the above command:
sudo find -type f -printf '%s %p\n'| sort -nr | head -10
To find out the largest files in a specific directory (Ex. /var) and its sub-directories just mention the path of the directory as shown below:
sudo find /var -printf '%s %p\n'| sort -nr | head -10
You have now basic idea about how to find the largest files and directories.
du -a -h –max-depth=1 | sort -hr
Find Smallest Directories And Files
To view the top ten smallest directories in the current working directory, run:
du -S . | sort -n | head -10
To view the smallest directories in a specific location, for example /var, run:
sudo du -S /var | sort -n | head -10
To view the top ten smallest files only in the current working directory, run:
ls -lSr | head -10
Likewise, to view the top ten smallest files only in a specific directory, for example /var, just run:
sudo ls -lSr /var | head -10