As a Linux administrator, you need to periodically check which files or folders occupy too much disk space. Periodically clearing disk space is not necessary. There are several ways to find the most common disk space directories and files in Linux.
The following command can find the maximum directory of the partition:
# du -a /home|sort -n -r|head -n 5
The Disk Usage (du) command is used to estimate the disk usage of files or folders. The following are some common du command options: -h or --human-readable: Display the size in an easy-to-read format, such as KB, MB, GB. -s or --summary: Displays only the total. --max-depth=N: Displays the size of the n-level subdirectories in the directory tree.
View the disk usage of all files and subdirectories in the current directory:
du -h --max-depth=1
To view the disk usage of a specified directory, including the total of subdirectories:
du -sh /path/to/directory
Statistics on the disk usage of all files and directories in the specified directory and sort them:
du -sh /path/to/directory/* | sort -hr
ncdu (NCurses Disk Usage) is a text-based user interface tool for viewing and analyzing disk space usage. It provides an interactive interface where you can browse through directories and see the size of each file and subdirectory.
Check the disk usage of the current directory:
ncdu .
The find command can be used to find files of a specific size to help identify files that take up a lot of space.
To find files larger than a certain size in the current directory and subdirectories:
find /path/to/directory -type f -size +100M
As long as the maximum file size is displayed you can use the command:
# find -type f -exec du -Sh {} + | sort -rh | head -n 5
To find the largest file in a specific location, simply include the path next to the command:
# find /home/tecmint/Downloads/ -type f -exec du -Sh {} + | sort -rh | head -n 5
OR
# find /home/tecmint/Downloads/ -type f -printf "%s %p\n" | sort -rn | head -n 5
The above command displays the largest file in the /home/tecmint/Downloads directory.
These commands help you find and analyze the most commonly used disk space directories and files on Linux systems. With these tools, you can quickly identify and process files and directories that take up a lot of disk space.