Linux Commands

How do I check the size of a Folder in Linux

Linux offers you various amazing command-line commands that will help in performing various tasks. One of the major tasks performed from the command line of Linux is to check the size of the file or folder. It might be necessary to make space for the incoming file or transfer it to another location. Also, when you run some deployments, it tends to create a log file automatically, and the space will fill up with those log files. So we must track the space of the file or folder and make space on the disk for them accordingly.

For this, you will need a Linux system, a command-line interface for running the command, and the right user access for checking the space on the Linux system.
We will be looking at various ways to check the file or folder size available on the Linux system.

Using du Command

The du in the “du” command specifies the disk usage, which is available in all Linux distributions by default. Run the below command for checking the disk usage for your Linux system.

du

You will get the output displaying your home directory contents and some numbers on the left showing the object’s size in KB.

If you want the output in a more readable format, use the “-h” option and the “du” command, as shown below.

du -h

In the above output, you will see some numbers ending with characters. The number specifies the amount of space used, and the character will specify the (usually K, G, or M) kilobyte, gigabyte, or megabyte.

If you want to check the size of the directory that is different from the current directory, use the following format for the “du” command.

du -h /var

The above command will help in displaying the size of the /var directory contents. You will notice an error in the below output as the user does not have the right permission for accessing that file. If you want the right privileges, use the sudo or su command as mentioned below.

sudo du -h /var

If you want to display the total disk usage for a particular directory, use the “-c” command as shown below.

sudo du -c /var

To get the output in a human-readable format, combine the “-c” option and the “-h” option, as shown below.

sudo du -hc /var

Well, you can even put the limit for displaying a certain level of the subdirectory with the help of the “max-depth” option. Consider the below example for displaying only the top level of the directory.

sudo du -hc --max-depth=0 /var

But if you want to specify the details of the top directory level along with the first layer of the subdirectory, run the below command with the max_depth value as “1,” as shown below.

sudo du -hc --max-depth=1 /var

Using Tree Command

Some Linux versions do not have the tree command; you can explicitly install it as follows.

  • For Linux distribution- Debian / Ubuntu
  • sudo apt-get install tree
  • For Linux distribution- CentOS / RedHat
  • sudo yum install tree

Using the tree command will display the visual representation of your directories. You can use the tree command along with several options for various functionalities. For getting the output in human-readable format, use the following command with the “-h” option.

tree -d -h

You can also use the tree command if you want to target the specific directory.

tree /var

If the provided directory has several contents, the above command will take time to generate the output.

Using ncdu Command

The ncdu is also known as NCurses Disk Usage, which is also not available in all the Linux distributions by default. You can explicitly install it using the below commands for various Linux distributions.

  • For Linux distribution- Debian / Ubuntu
  • sudo apt-get install ncdu
  • For Linux distribution- CentOS / RedHat
  • sudo yum install ncdu

Use the ncdu command for getting the interactive display of the disk usage.

ncdu

You can see the current directory being scanned in the upper right corner. In the left column, you will get the numerical size along with a graph of #- signs specifying the relative size.

For selecting the different lines, you can use the up and down arrows. With the right arrow, you will be able to browse into the directory and using the left arrow, you can go back.

You can also use the ncdu command for targeting the specific directory.

ncdu /var

For any help with the ncdu command, you can press the “?” key inside the ncdu interface. If you want to quit, you can press the “q” letter.

Conclusion

While working on a Linux system, you must know the usage of a disk and the size of your files or directories. You can use various commands for the purpose that we have mentioned above. You can even create a bash script for emptying the file if the size exceeds a certain limit using the disk usage commands. Once you start working on a Linux system, you will check disk space commands.

About the author

Simran Kaur

Simran works as a technical writer. The graduate in MS Computer Science from the well known CS hub, aka Silicon Valley, is also an editor of the website. She enjoys writing about any tech topic, including programming, algorithms, cloud, data science, and AI. Travelling, sketching, and gardening are the hobbies that interest her.