How to Get the Size of a Directory in Linux

Introduction

Getting the size of a directory in Linux is a simple task that can be accomplished with a few commands. Knowing the size of a directory can be useful for a variety of reasons, such as determining how much space a directory is taking up on a hard drive or how much space is available for new files. In this guide, we will discuss how to get the size of a directory in Linux using the du command. We will also discuss how to get the size of a directory recursively, which is useful for getting the size of all the files and subdirectories within a directory.

How to Get the Size of a Directory in Linux

[ad_1]

Introduction

Many users run Linux from the command line. However, the command line – sometimes known as the terminal – doesn’t have an intuitive interface for checking disk space in Linux.

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

Tutorial on how to find directory size linux

Prerequisites

  • A system running Linux
  • A command line / terminal window (available by clicking Search, then typing terminal)
  • A user account with sudo or root privileges

Note: In Linux, a directory is the equivalent of a folder in Windows. A directory may have directories inside (called subdirectories), or it may only contain files.

Option 1: Display the Size of a Directory Using the du Command

The ducommand stands for disk usage. This command is included by default in most Linux distributions.

You can display the size of your current directory by typing du in the command line:

du

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

An example of the du command output.

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

du -h
Readable list of contents of your home directory.

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 examine:

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.

Display the size of a specific directory.

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

Note: Some versions of Linux don’t enable sudo by default. You can use the su command to switch to the root user account instead.

To display total disk usage of a particular directory, use the -c command:

sudo du -c /var

Options can be combined. If you wanted to repeat the previous command in human-readable format, enter the following:

sudo du -hc /var

You can limit the scan to a certain level of subdirectory by 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 wanted to list only the top directory and the first layer of subdirectories, change --max-depth=1:

sudo du -hc --max-depth=1 /var
Find the size of the top directory

If you run into trouble or want to explore more options for the du command, enter the following command to display the help file:

man du

Option 2: Get Size of Directory in Linux Using tree Command

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

sudo apt-get install tree
sudo yum install tree

The tree command displays a visual representation of your 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 current directory’s subdirectories, enter the following:

tree -d -h
Display disk usage using the tree command.

Like the du command, tree can target a specific directory:

tree /var

This command takes a few moments since 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 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:

sudo apt-get install ncdu
sudo yum install ncdu

The ncdu utility is an interactive display of your disk usage. For example, enter the following:

ncdu
Display Disk Usage Using the ncdu command.

In the upper left corner, it displays the current directory being scanned. A column on the left displays 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 browse into a directory, and the left arrow will take you back.

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

ncdu /var

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

Conclusion

You now have three different options to find the size of a directory in Linux operating systems.

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

[ad_2]

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

GSTIN is 03EGRPS4248R1ZD.

Contact
Jassweb, Rai Chak, Punjab, India. 143518
Item added to cart.
0 items - 0.00