This article is part of the following series.
- 2 ways to create a directory in Linux
- How to Get the current directory in Linux
- Linux File System: Understanding Directory Structure and Navigating the File System
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!
Boost Your Website Speed!
If you want your website to run as fast as ours, consider trying Cloudways. Their powerful cloud infrastructure and optimized stack deliver exceptional performance. Free migration!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.
Table of Contents
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)
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.
We could make this easier just adding 2 flags. Flags help the command change its behavior. For the du command we could add the flags -s and -h.
- -s stands for summarize and it will show you only the total size of the directory, not the sizes of individual files.
- -h stands for human-readable and it will convert the size so that you can read it easier. Just by running the command without the -h flag doesn’t specify you a unit of measurement.
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