1.7K
Finding the largest directories in a Linux system is like playing detective in a world of data. Itâs not just about freeing up disk spaceâitâs about understanding where your resources are being used and making informed decisions about what to keep, what to archive, and what to delete.
In this blog, Iâll walk you through various ways to unearth the heavyweight directories lurking in your Linux system. Letâs dive in, and yes, weâll sprinkle a bit of my personal spice along the way.
The du command: The traditional heavyweight
The du (disk usage) command is the old reliable of finding out how much space a directory is using. Itâs like that old detective coat you canât part withânever fails you. Hereâs how to use it:
Basic usage
du -sh /path/to/directory
The -s option gives you a summary instead of listing every file, and -h makes the output human-readable (who really counts bytes these days?).
Finding the top offenders
But what if we want to find the largest directories within a certain path? Enter the sorting magic:
du -h /path/to/directory | sort -rh | head -n 10
This command will list the top 10 heavyweight directories in descending order. The sort command is our friend here, with -r reversing the order (because we want the largest, not the smallest) and -h handling human-readable numbers (yes, it knows that 1M is larger than 1K).
Output
1.5G /path/to/directory/subdir1
1.2G /path/to/directory/subdir2
800M /path/to/directory/subdir3
…
Ah, the satisfaction of seeing exactly where your disk space has gone. Priceless. Let me give you a practical example. To list the sizes of directories in the current working directory and sort them to find the largest, you can use the following command:
du -h –max-depth=1 | sort -rh | head -n 5
Example:
1.5G ./dir1
1.2G ./dir2
800M ./dir3
600M ./dir4
450M ./dir5
The graphical approach: For the visually inclined
Not everyone loves the terminal (though I canât imagine why). For those who prefer a graphical interface, thereâs Baobab, the Disk Usage Analyzer in Ubuntu.
baobab command usage in linux
Simply open it from your applications menu, and youâll be presented with a visual breakdown of your disk usage. Itâs like a map to treasure, but instead of treasure, itâs data you probably forgot about.
While I personally prefer the terminal (thereâs something about text output that just feels more like hacking), I canât deny the appeal of seeing your data usage laid out in colorful graphs.
The ncdu command: A modern twist
For those who want a bit of both worldsâterminal-based but with an easy-to-navigate interfaceâncdu (NCurses Disk Usage) is the answer. Itâs like du went to the gym and came back with a new UI.
Installation steps:
Debian/Ubuntu and derivatives
sudo apt-get update
sudo apt-get install ncdu
Fedora
sudo dnf update
sudo dnf install ncdu
CentOS/RHEL
For CentOS or RHEL 7 and below, you might need the EPEL repository:
sudo yum install epel-release
sudo yum update
sudo yum install ncdu
For CentOS/RHEL 8 and newer versions, use dnf instead:
sudo dnf install epel-release
sudo dnf update
sudo dnf install ncdu
Arch Linux
sudo pacman -Syu
sudo pacman -S ncdu
openSUSE
sudo zypper refresh
sudo zypper install ncdu
Alpine Linux
sudo apk add ncdu
Gentoo
sudo emerge –update –newuse ncdu
And run it with:
ncdu /path/to/directory
Youâll be greeted with a navigable interface showing your directories, their sizes, and you can even delete files from within ncdu. Be careful, though; with great power comes great responsibility.
Example:
FAQ: Finding the largest directories in Linux
Can I use the du command on the entire system?
Yes, you can, but be prepared to wait because itâll take some time. Use du -sh / to see the total disk usage of your root directory, but remember, this scans everything, so it might take a while.
How can I exclude a specific directory when using du?
If you want to exclude a directory, you can use the –exclude flag. For example, du -sh –exclude=/path/to/exclude /path/to/directory will calculate the size of /path/to/directory without including the specified excluded path.
Is ncdu better than du?
âBetterâ is subjective. ncdu offers a more user-friendly interface and interactive usage, making it easier for some users to navigate and manage files directly. du, on the other hand, is straightforward and perfect for quick, scriptable commands. Your preference will depend on your needs and how you like to work.
Can Baobab scan network locations?
Yes, Baobab can scan network locations, but the performance and accuracy can depend on the networkâs stability and the remote file systemâs characteristics. Use File > Connect to Server in Baobab to add a network location.
What does the sort -rh command do?
The sort -rh command sorts the output in human-readable format (-h, where 1K is less than 1M) and in reverse order (-r), ensuring that larger sizes appear at the top of the list.
How do I install ncdu if itâs not available on my system?
If ncdu is not already installed, you can install it using your distributionâs package manager. For Ubuntu or Debian-based systems, the command is sudo apt-get install ncdu. For Red Hat-based systems, use sudo yum install ncdu.
Can I find the largest files instead of directories?
Absolutely. While the methods mentioned focus on directories, you can find large files using the find command. For example, find /path/to/search -type f -exec du -h {} + | sort -rh | head -n 10 will list the top 10 largest files in the specified path.
How can I monitor disk usage over time?
To monitor disk usage over time, you might need to use additional tools or scripts. One simple approach is to run a du command periodically (via cron jobs, for example) and save the output to a file for later analysis. There are also more sophisticated monitoring solutions available that can track disk usage trends.
Conclusion
There are many ways to find the largest directories in your Linux system, each with its own charm. I personally lean towards ncdu for its balance of power and usability, but Iâve been known to run a quick du command just for the nostalgia.
So, go ahead, play detective with your file system. You might be surprised at what you find hiding in the depths of your directories.