Chat Zalo Chat Messenger Phone Number Đăng nhập
How to Check the Size of a Directory in Linux {3 Options}

How to Check the Size of a Directory in Linux {3 Options}

Introduction

Many users run Linux from the command line. However, the command line, sometimes referred to as terminal, does not have an intuitive interface for checking disk space in Linux.

This guide shows how to find the size of a specific directory in Linux from the command line.

Prerequisites

A

  • system running Linux
  • A

  • command-line/terminal window (available by clicking Search and then typing terminal)A
  • user account with sudo or root privileges

Option 1: Display the size of a directory by using the

du command

The du command means disk usage. This command is included by default in most Linux distributions.

You can display the size of your

current directory by typing du at the command line: du

The system should display a list of the contents of your home directory, with a number on the left. That number is the size of the object in kilobytes.

You can add the -h option to make the output more readable: du -h

Each entry will start with a number and a letter. The number is the amount of space used, and the letter (usually K, M, or G) indicates kilobytes, megabytes, or gigabytes. For example:

400K – 400 kilobytes 7.3M – 7.3 megabytes 2.2G – 2.2 gigabytes

To find the size of a specific directory different from your current working directory. The du command allows you to specify a directory to browse: du

-h /var

This displays the size of the contents of the /var directory. You may see some entries with an error, as in the image below.

This happens when your user account does not have permission to access a particular directory. Use the sudo or su command to get access privileges: sudo du -h /var To display the total disk usage of a particular directory, use the -c: sudo du -c /var

The

options can be combined. If you want to repeat the above command in human-readable format, type the following:

sudo du -hc /var

You can limit the scan to a certain subdirectory level using the max-depth option. For example, to scan only the size of

the top directory, use -max-depth=0: sudo du -hc -max-depth=0 /var If you want to

list only the top directory and the first layer of subdirectories, change -max-depth=1: sudo du -hc –max-depth=1

/var

If you have problems or want to explore more options for the du command, Enter the following command to display the help file

: man du

Option 2: Get the directory size

in Linux using the tree command

By default, the tree command is not included in some versions of Linux. To install it, enter the following:

For

  • Debian/Ubuntu

sudo apt-get install tree

  • For CentOS/RedHat

sudo yum install tree

The tree command displays a visual representation of the directories. It uses lines to indicate which subdirectories belong where, and it uses colors to indicate directories and files.

Tree can also be used with options. To display a human-readable size of the subdirectories in the current

directory, type the following: -d -h tree Like the du command, tree can point to a specific directory: /var tree This command

takes a few minutes because the /var directory has many entries.

The

tree command

also has a help file, which you can access by entering

: man tree

Option 3: Find the size of a Linux directory using the

ncdu command

The ncdu tool stands for NCurses Disk Usage. Like the tree command, it is not installed by default on some versions of Linux. To install it, enter the following:

For

  • Debian/Ubuntu

sudo apt-get install ncdu

  • For CentOS/RedHat

sudo yum install ncdu

The ncdu utility is an interactive disk usage screen. For example, type the following:

ncdu

In the upper-left corner, it displays the current directory being scanned. A column on the left shows the numerical size, a graph of #- signs to indicate the relative size, and the file or directory.

Use the up and down arrows to select different lines. The right arrow will navigate into a directory, and the left arrow will take you back.

ncdu can be used to point to a specific directory, for example:

ncdu /var

For help, press the ? key inside the ncdu interface. To exit, press the letter q.

Conclusion

You now have three different options for finding the size of a directory on Linux operating systems

.

If you want to learn more about directories in Linux, read our article how to rename directories in Linux.

Contact US