howtouselinux

5 Ways to Check directory size in Linux

Table of Contents

 

Linux is a powerful operating system that offers many features and options for users. One of the things that Linux users can do is check the size of directories on their system. In this blog post, we will discuss five 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!

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, and they can be removed using the rmdir command. To change into a directory, use the cd command.

Find directory size with du -sh command in Linux

The best Linux command to check directory size is using du -sh command.  Open the terminal and type du -sh directory name 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 sizes in Bytes, Kilobytes, Megabytes, Gigabytes, etc.

  • -s, –summarize : display only a total size
  • -h, –human-readable : print sizes in human readable format (e.g., 1K 234M 2G)

 

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

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

 

Get directory size with du -ah command in Linux

The du command can also be used with the -a and -h options to check the size of directories and files in human-readable format.

  • -a, –all : write counts for all files, not just directories
  • -h, –human-readable : print sizes in human readable format

 

For example, the following command will check the size of all the files and directories under /etc directory and display the output in human-readable format: $ du -ah /etc

# du -ah /etc/
4.0K /etc/aliases
12K /etc/aliases.db
0 /etc/alternatives/libnssckbi.so.x86_64

Check directory size with du Command -–exclude option in Linux

Another way to check the size of directories in Linux is to use the du command with the -exclude option. This option will exclude directories 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 Command -xh option 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 directories. 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 -printf “%s\n”

The output of this command will be something 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.

FAQ about directory in Linux

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 all files and directories in a given directory. The output of the du command is in kilobytes, megabytes or gigabytes.

  • To find out the size of a specific file: du my_file
  • To find out the size of a specific directory: du my_directory
  • To find out the size of all files and directories in a given directory: du -a my_directory/*

 

How to create, change and remove directories in Linux?

  • To create a new directory: mkdir my_new_directory
  • To change into a directory: cd my_new_directory
  • To list the contents of a directory: ls -l
  • To find out your current working directory: pwd

 

How to find out what is taking up so much space in my home directory?

There are a few ways to find out what is taking up so much space in your home directory. You can use the “du” command to check the size of each file and directory. Or, you can use the “find” command to list all of the files that are larger than a certain size.

Linux command related to directory

  • The cd command is used to change into a different directory.
  • The pwd command is used to print your current working directory.
  • To move up one level in the directory tree, use the cd .. command. To move to your home directory, use the cd ~ command.
  • To move to a specific directory, use the cd /path/to/directory command. Remember to use proper pathnames when using the cd command. For example, if you want to move into the /usr/local directory, you would use the following command: cd /usr/local

 

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Liquid Web Managed Word Press