howtouselinux

4 Ways to Check directory size in Linux

Table of Contents

Linux is a powerful operating system that offers many features and options for users. In this blog post, we will discuss four different ways to check directory size in Linux. We will also provide tips and tricks for each method.

So, whether you are a beginner or an experienced Linux user, you should be able to find a method that works for you!

Get directory size with Linux commands

The following Linux commands will get you the directory size.

  • du -sh directory: get a summary of the directory’s size in a human-readable format.
  • du -ah directory: get the size of sub-directories and files in human-readable format under this directory
  • du -sh –exclude directory1 directory: get the size of the directory excluding directory1
  • du -shx directory: get the size of the directory excluding the size of directories on a different file system
  • find /etc -maxdepth 0 -type d -exec du -sh {} \;

 

Procedure to check directory size in Linux

The procedure to check directory size in Linux is as follows:

  • Open the terminal application.
  • Type du -sh /dir
  • Press Enter to run the command.
  • The output will display the size of this directory
  • du -s option will display only a total size
  • du -h option will print directory size in human readable format (e.g., 1K 234M 2G)

 

Check directory size with du -sh command in Linux

The most efficient way to check directory size in Linux is using du -sh command.  Open the terminal and type du -sh directory path in the prompt. The directory size will be listed on the first column.  The size will be displayed in Human Readable Format. This means we can see directory size in Bytes, Kilobytes, Megabytes, Gigabytes, etc.

For example, the following command will get the size of the /etc directory: $ du -sh /etc . The output of this command will be something like this: 18M /etc

  • du -s option will display only a total size
  • du -h option will print directory size in human readable format (e.g., 1K 234M 2G)

 

If you want to get the size of the current directory, we would type: du -sh. This will give us a human-readable output of the size of the current directory.

Example:
# du -sh 
27M .

If you want to get the size of all the files and directories in the current directory, you can use the du -sh command with the * wildcard. This will give us a human-readable output of the size of all the files and directories in the current directory.

Example:
# du -sh *
4.0K adjtime
4.0K aide.conf
8.0K aide.conf_template
4.0K aliases
12K aliases.db
4.0K alternatives

We can combine this with sort command. du -sh *|sort -h. It is quite useful when you try to find the largest files or directories under the current directory. The largest one will be displayed on the bottom of the output.

Example:
du -sh *|sort -h
244K vmware-tools
596K ssh
680K services
1.2M pki
3.9M filebeat
8.8M udev
9.3M selinux

 

Without any options in du command, it will display the size of all the directories in a given directory.  For example, du /etc will list the size of all the directories under /etc directory and subdirectories.

For example:

# du /etc
12 /etc/dnf/modules.d
0 /etc/dnf/protected.d
0 /etc/dnf/aliases.d
0 /etc/dnf/modules.defaults.d
0 /etc/dnf/plugins/copr.d
20 /etc/dnf/plugins
0 /etc/dnf/vars
36 /etc/dnf
0 /etc/fonts/conf.d
0 /etc/fonts
12 /etc/rhsm/ca

 

Check directory size with du –exclude command in Linux

We can use exclude option to exclude a file or directory from the output in the du command. Use the “–exclude” option followed by the path to the file or directory you want to exclude. This will exclude directories or files from the output.

For example, if we want to check the size of the /usr directory but we don’t want to include the /usr/local directory in the output, we can use the following command:

$ du -sh –exclude /usr/local /usr

The output of this command will be something like this: 12M /usr

Get directory size with du -sxh command in Linux

The du command can also be used with the -x and -h options to exclude the size of directories on a different file system.

For example, if we want to check the size of the /home directory but we don’t want to include any subdirectories that are on a different file system, we can use the following command: $ du -shx /home

 

Find directory size with find Command in Linux

The find command is another Linux utility that can be used to find files and directory. To use the find command, you will need to specify the path of the file or directory that you want to check. For example, the following command will check the size of the /etc directory:

$ find /etc -maxdepth 0 -type d -exec du -sh {} \;

The output of this command is like this:18M. This output tells us that the /etc directory is 18 megabytes in size.

What is the difference between du and df in Linux?

The “du” command shows you the amount of disk space being used by the files or directory in a directory. The “df” command shows you the amount of free disk space on your system.

How to reduce directory size in Linux

There are a few ways to reduce the size of a directory in Linux. You can delete files, move files to a compressed archive, or remove empty directories.

There are a few directories that you should regularly clean in Linux. The /tmp directory can be cleared with the help of the tmpwatch utility. The /var/log directory can be cleaned with the help of logrotate. The /home/USERNAME directory can be cleaned with the help of the cleanup utility.

FAQ about directory in Linux

What is a directory in Linux?

A directory is a file system container that can hold files and other directories. When we talk about the size of a directory, we are referring to the amount of disk space that is being used by the files in that directory.

Directories are created using the mkdir command. To change into a directory, use the cd command.

If we need to check the size of files and sub-directory under /etc/, we can use du -sh /etc/*.
# du -sh /etc/*
4.0K /etc/adjtime
8.0K /etc/aide.conf
4.0K /etc/aliases
12K /etc/aliases.db
4.0K /etc/alternatives

 To check the size of the directories under /etc, we can use du -h –max-depth=1 /etc/

What is du command in Linux?

The du command in Linux is used to find out the size of a file or directory. It can also be used to find out the size of files and directories in a given directory. The output of the du command is in kilobytes, megabytes or gigabytes.

  • find out the size of a specific file: du my_file
  • find out the size of a specific directory: du -sh my_directory
  • find out the size of all files and directories in a given directory: du -a my_directory/*
  • Show totals for current folder only: du -s *
  • View the space usage of the specified directory level in a convenient way: du -lh –max-depth=1

 

 

 

David Cao
David Cao

Hey there! I am David, a Cloud & DevOps Enthusiast and 18 years of experience as a Linux engineer. I work with AWS, Git & GitHub, Linux, Python, Ansible, and Bash. I am a technical blogger and a Software Engineer, enjoy sharing my learning and contributing to open-source.