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
down arrow keys). To expand a particular directory, press
Enter. To delete a file or directory, press the
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.
.– scan the current directory
a– consider both the size of files and directories
h– convert the size to a human-readable format
x– skip directories on different file systems
sort -rh– sort results
head -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