There are several tools you can use to quickly find out which files and directories are taking up the most space on your Linux machine. In this post, I will show the methods I use most to find out what takes up free space in Linux.
Ncdu
is the most popular tool to get file and directory sizes in Linux. It is not installed by default but is available in all of the default repositories of the most popular Linux distros.
To install ncdu on Ubuntu or Debian, run the command:
$ sudo apt install ncdu
On Orace/Rocky Linux/CentOS/RHEL:
# yum install ncdu.
To find out the size of all objects in a specified directory, you can run the following command:
$ sudo ncdu /var
You will see a list of files, directories, and their sizes. The ncdu tool allows you to navigate through the list of objects (using the up
and down arrow
keys). To expand a particular directory, press Enter
. To delete a file or directory, press the d
key.
If you have the MC (Midnight Commander)
file manager installed on your host, you can display the directory size by pressing Ctrl + Space
.
If there are no other tools available on the server, you can use the built-in du
command to find out the size of the files and folders on the hard drive.
For example, the following command lists the 10 largest files or folders in the current folder:
$ sudo du -ahx . | sort -rh | head -10
The first column shows the size of the file system object, the second column shows the name.
Options used:
.
– scan the current directorya
– consider both the size of files and directoriesh
– convert the size to a human-readable formatx
– skip directories on different file systemssort -rh
– sort resultshead -10
– show only the first 10 entries
Get the size of objects in a specified directory:
$ du -ahx /var | sort -rh | head -10
If you only want to scan objects with a nesting level of 1::
$ du /var/* --max-depth=1 -ahx |sort -hr
Top 20 biggest dirs:
# du -hcx –max-depth=6 / 2>/dev/null | sort -rh | head -n 20
Top 20 biggest files:
# find / -mount -ignore_readdir_race -type f -exec du -h “{}” + 2>&1 \
Top 20 oldest files:
# find / -mount -ignore_readdir_race -type f -mtime +30 -exec du -h “{}” + 2>&1 \