Skip to Content

2 effective ways to find the largest directories in Linux

Do you need to find the largest directories on your Linux system? There are two ways that you can do this. In this blog post, we will show you both methods. The first method is to use the find command, and the second method is to use the du command. Let’s get started!

Find the largest directories with find command in Linux

The easiest way to find the largest directories in Linux is using this command find /dir -maxdepth 1 -type d -exec du -sh {} \;|sort -rh|head -10. The find command can be used to search for files or directories that match a certain criteria.

The procedure to find the largest directories in Linux is as follows:

  • The find command is a powerful tool that allows you to search for files or directories based on a variety of criteria.
  • The -maxdepth 1 option tells the find command to only look in the current directory and not its subdirectories. Check more about depth in Linux find command
  • The -type d option tells the find command to only look for directories.
  • The -exec du -sh {} \; command will execute the du command on each of the directories that it finds.
  • The sort -rh command will sort the directories by their sizes, with the largest directories first.
  • The head -10 command will only show top 10 largest directories in /dir/

 

Get Your Free Linux training!

Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!

For example, the following command will get the largest 10 directories under /etc/ directory.

find /etc -maxdepth 1 -type d -exec du -sh {} \;|sort -rh|head -10
23M /etc
9.3M /etc/selinux
8.8M /etc/udev
1.2M /etc/pki
604K /etc/ssh
224K /etc/postfix
164K /etc/sysconfig
140K /etc/lvm
112K /etc/cloud
96K /etc/profile.d

The find command can also be used to find files that are a certain size. If you wanted to find all of the directories that are larger than 100 MB, you would run the following command:

find / -type d -size +100M

This command will find all of the directories that are larger than 100 MB. The -type d option tells the find command to only look for directories. The -size +100M option tells the find command to only look for files that are larger than 100 MB.

Related: 3 Ways to find largest files in Linux

Find the largest directories with du command in Linux

Another way to find the largest directories in Linux is using this command du -h -d 1 /dir/ |sort -rh|head -10. The du command is used to show the disk usage of the directory. The sort command will sort directories by their sizes. The head command will only show top largest directories in /dir/

Let’s break down this command.

  • specify the path of the directory that you want to check in du command
  • The -d option in the du command tells the command to only check the directories that are at level 1. This can be useful if you only want to see the size of a specific directory not its subdirectories. If you don’t use the -d option, the du command will check the size of all of the directories and subdirectories in the path that you specify.
  • The -h option in the du command will show you the size of the directory in human-readable format.
  • The sort -rh command will sort directories by their sizes, with the largest directories first. This can be useful if you want to find the largest directories on your Linux system.
  • The head command will only show top 10 largest directories in /dir/

 

We can use this command to find the largest directories under /etc directory.
# du -h -d 1 /etc/ |sort -rh|head -10
23M /etc
9.3M /etc/selinux
8.8M /etc/udev
1.2M /etc/pki
604K /etc/ssh
224K /etc/postfix
164K /etc/sysconfig
140K /etc/lvm
112K /etc/cloud
96K /etc/profile.d

if you wanted to check the size of the /home/user/directory, you would run the following command: du -sh /home/user/directory

This command will show you the size of the directory in human-readable format. The -s option tells du to get a summary of the directory’s usage.

Both of these methods can be used to find the largest directories in Linux. Try both methods and see which one works better for you. Thanks for reading! We hope this blog post has been helpful.

Troubleshooting in Linux: Why is the ls Command Lagging? - howtouselinux

Thursday 7th of December 2023

[…] usage. If it’s nearly full, you may need to free up some space. You can use the du command to find out which directories are taking up a lot of space and then delete unnecessary […]