Skip to Content

4 Ways to Check the size of a directory in Linux

This article is part of the following series.

 

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!

If you are new to Linux, you can refer to this article: Mastering the Linux Command: A Beginner’s Guide. This guide will equip you with the essential knowledge and skills to become proficient in using the command line interface (CLI) effectively.

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,not the sizes of individual files
  • du -h option will print directory size in human readable format (e.g., 1K 234M 2G)

 


 

Linux course for Beginners

There are many reasons why it is valuable for you to learn Linux. If you are looking for new opportunities in IT, Linux skills are in high demand.

For example, if you are developing applications, it’s likely your application or its runtime is hosted on Linux. If you’re working in the cloud, your cloud instances may be based on Linux, and your private or public cloud environment is also probably based on Linux.

We highly recommend this course on Coursera to Linux beginners. Coursera offers a variety of Linux courses from top universities and organizations. Coursera also offers specializations in Linux and cloud computing.

This course will provide you with a basic introduction to Linux® skills using Red Hat® Enterprise Linux 8. It will show you how a Linux system is organized, and will demonstrate introductory system administration tasks, which you will be able to practice on your own.

What you will learn from this course

  • Getting Started with Red Hat Enterprise Linux
  • Accessing the Command Line
  • Managing Files From the Command Line
  • Creating, Viewing, and Editing Text Files
  • Managing Local Linux Users and Groups
  • Controlling Access to Files with Linux File System Permissions
  • Monitoring and Managing Linux Processes
  • Installing and Updating Software Packages
  • Comprehensive Review

 

After completing this course, you will have a solid introduction to working with Linux from the command line, using Red Hat Enterprise Linux 8 as a model. In applying these skills, you will be able to perform fundamental operational tasks, whether your Linux machine is on your desk or on a remote system across the Internet.

Top reviews

BS Jul 29, 2022
Awesome experience with my first course at Coursera. I really enjoyed the whole course and it was fun to play with the linux commands in AWS EC2. Thank you so much for such great content.
Completed this course and obtained my first certification in Linux. It was very fun and engaging. I would recommend anyone looking at getting into Linux

AV Jun 30, 2021
As someone new to the Linux environment, I have to say this course was excellent. The lessons and documentation were easy to read and understand, the labs were informative and easy to follow.

The presenter was well spoken and easy to understand.At no point in time did I feel overwhelmed or clueless. Definitely one of the best courses I’ve done on this site.

Learn more details about this course and see how it can help you level up your Linux skills

Check the size of a directory with du -sh command in Linux

From Redhat official website, 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: 18M /etc

Without using -sh option, du command will give you the size of all the files in this directory and at the very end, it will give you the size of the directory itself.

 

Check the size of current directory in Linux

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 to sort files by size. What | does is get the output from the command behind it and add it into the input of the command after it.

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 the size of a directory 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

Here is what each option in the command does:

  • du: This is the command for “disk usage”.
  • -s: This option tells du to only display the total size of the directory, not the sizes of individual files.
  • -x: This option tells du not to include files and directories on different file systems.
  • -h: This option displays the output in a human-readable format, with units such as “K” for kilobytes and “M” for megabytes.

 

You can tell which file system a file or directory is using by using the df command.

To use the command, simply type df followed by the file or directory path you want to check. For example, if you want to check if the /home directory is on a different file system, you can type:

df /home

The output will display the disk usage information for the file system that contains the /home directory, and it will include the file system’s device name, the total size of the file system, the amount of used space, the amount of available space, and the file system’s mount point.

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.

Get the size of a directory with Linux commands

 

  • 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 {} \;

 

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 -sh *
  • View the space usage of the specified directory level in a convenient way: du -lh –max-depth=1