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.
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.
1 comment
hi,
thanks a lot,
very nice and useful article