When working with Linux systems, it’s common to come across directories that are taking up a lot of space on your hard drive. This can be a problem if you’re running low on disk space, or if you want to clean up your system and remove unnecessary files. In this article, we’ll show you how to use the
find command to locate the largest directories on your Linux system.
What is the
find is a powerful command-line tool that allows you to search for files and directories based on various criteria, such as name, type, size, and more. The
find command is available on most Unix-based systems, including Linux.
The basic syntax of the
find command is as follows:
find [path] [expression]
[path] specifies the starting directory for the search, and
[expression] specifies the search criteria. The
find command will search for all files and directories that match the criteria and print their names to the console.
How to Find the Largest Directories in Linux
To find the largest directories on your Linux system, you can use the
du command to calculate the disk usage of each directory, and then pipe the output to the
sort command to sort the results by size. Finally, you can use the
head command to display the top N directories.
Here’s the basic command to find the top 10 largest directories on your Linux system:
du -h / | sort -hr | head -n 10
Let’s break down the command:
du -h /calculates the disk usage of all directories under the root directory (
/), and prints the results in human-readable format (
sort -hrsorts the output of
duin reverse order (
-r) based on the size of each directory (
head -n 10displays the top 10 largest directories.
You can adjust the number of directories displayed by changing the value of
n in the
Understanding the Output
When you run the
du command, it will print the disk usage of each directory in bytes. When you pipe the output to the
sort command, it will convert the sizes to a more human-readable format (e.g. 1K, 1M, 1G) and sort the results based on the size.
Here’s an example output of the
1234567 /var/log 987654 /usr/share
This means that the
/var/log directory is using 1234567 bytes (or approximately 1.2 MB) of disk space, and the
/usr/share directory is using 987654 bytes (or approximately 0.9 MB) of disk space.
Filtering Directories by Size
If you want to find directories that are larger than a certain size, you can use the
find command with the
-size option. For example, to find all directories under the root directory (
/) that are larger than 1 GB, you can run the following command:
find / -type d -size +1G
Let’s break down the command:
find /searches for all files and directories under the root directory (
-type dspecifies that we’re only interested in directories.
-size +1Gspecifies that we’re only interested in directories that are larger than 1 GB.
You can adjust the size threshold by changing the value after the
+ sign. For example,
+100M would find directories larger than 100 MB.
In this article, we’ve shown you how to use the
find command to locate the largest directories on your Linux system. By using the
head commands together, you can quickly find the directories that are taking up the most space on your hard drive. We’ve also shown you how to filter directories by size using the
find command. With these tools, you can easily manage your disk space and keep your Linux system running smoothly.