How to Get Directory Size in Linux [4 Methods Inside!]

Size of a Directory in Linux

Imagine your Linux system as a huge storage area divided into sections called directories (or folders if you are coming from Windows). 

As you use your system and add more files, these directories get bigger. In extreme cases, huge directory sizes can slow down system operations because of the time and resources required to parse them and find relevant information or resources.

Knowing how to find the size of directories is essential to keeping your Linux system running smoothly and ensuring you have enough disk space. This will help you avoid operational issues such as system slowdowns and running out of space for critical data. 

Like Windows, where size of a folder is a critical piece of system information, knowing the folder size is important for Linux system administration. 

In this comprehensive tutorial, we’ll cover four essential methods (ls, du, ncdu, and tree commands) for determining how much space directories take up on the system storage. Finally, we will explore the role of file managers in this context. 

Prerequisites

  • A system running a mainstream Linux distribution
  • Access to a command line or terminal
  • A user account with root privileges.

Method #1: How to Use the du Command to Find Linux Folder Size

The du (short for disk usage) command tells you how much space directories consume. It is already included in the core Linux utilities and available in all mainstream distributions.

Run the following command in the terminal to check the size of the current directory.

# sudo du

 du

The output of the command shows all files in the current directory with their size in kilobytes (KB). Usually, these numbers are too huge to be useful. You can include the -h flag to improve the readability of file sizes.

# sudo du -h

sudo du -h

You can pass the path of a directory to the du command to see the size details.

# du -h /var

Note that if your account doesn’t have access permission to a file, you’ll see the following error instead of the directory listing:

du -h /var

To resolve this error, simply use sudo with the du command to gain access to the directory and its contents:

# sudo du -h /var

sudo du -h /var

You can use the -c flag to view the total disk usage of a specific directory:

#sudo du -c /var

Now, let’s combine the -c and the -h flags in the following command format:

# sudo du -hc /var

You’ll get the same result here as above.

How to Restrict Directory Scan Levels in the du Commands

By default, the du command traverses the entire directory. You can imagine the time required to process a multi-level folder. You can restrict the scan to a specific subdirectory level using the idea of du one level. 

This is implemented with the max-depth option. 

For instance, use –max-depth=0 to scan only the top directory’s level:

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

You can extend the depth of the scan to display only the top directory and the first level of subdirectories. The following command will help you understand the idea of Linux du one level by changing the command as follows:

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

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

How to Get Help for the du Command?

The du command is very versatile and you can get very precise information about disk utilization by modifying it with flags and options. You can get detailed information about these options with the following man command: 

# man du

You can use this command without sudo, as it doesn’t require root privileges to display manual documentation.

Include Hidden Files and Directories in the du Results

Now that you know how to use the du command, let’s discuss the idea of Linux du hidden directories. 

By default, du includes the hidden directories and files in its results.  For instance, if you run the du sh command, hidden files and directories are automatically included in the output. The command to list du hidden files in this context is as follows:

# du -sh

Alternatively, you can use the following variation to list all files (including du hidden files) and directories in the result:

# du -ak

Method #2: Use the tree Command to Check Directory Size in Linux

The tree command shows a graphical representation of the directories. It differentiates folders and files using colors and shows the connection between the subdirectories and files with lines.

tree is very versatile, and you can use several flags to modify the output of the command. For instance, use the following command to see the subdirectories’ sizes in the current directory in a human-readable format:

# tree -d -h

tree -d -h

You can also pass a directory path to the tree command to view the structure of the directory:

# tree /var

How to Get Help for the tree Command?

You can access the details and available options for the tree command by entering:

# man tree

Important: In some Linux distributions, the tree command is unavailable by default. You can install it with the package manager of your distribution.

For instance, you can install it on Ubuntu (and other Debian-based distributions) by running the following command:

# sudo apt-get install tree

sudo apt-get install tree

Method #3: Use the ncdu Command to Get Directory Size in Linux

The ncdu (NCurses Disk Usage) tool is similar to the tree command in the level of details of the command’s output.

The utility displays the disk usage in detail. The typical usage is simply calling the utility at the command line:

# ncdu

ncdu

You’ll see the files and directories, their sizes in KB (by default), and a column of #s indicating their relative sizes.

You can easily navigate the output with the Up and Down arrows to select a line. Press the Right arrow to move forward in the folder and the Left arrow to return.

You can pass the path to a directory to the ncdu command to view its contents and sizes.

# ncdu /var

ncdu /var

How to Install ncdu on Your System

Since ncdu is not available in all distributions, you might need to install it if it’s not available on your system. If you’re using a Debian-based distro, run the following command:

# sudo apt-get install ncdu

sudo apt-get install ncdu

Method #4: Use the ls Command to Find the Directory Size in Linux

You can use the standard ls command to list the size of the folders and files on your system. The output of the command will show the size of the files and directories. 

Note that the ls command is primarily designed for listing files in a directory. As such, the ls folder size is not very accurate. We strongly recommend using the du or ncdu utilities for a more estimation of the directory sizes in Linux environments.  

Run the following command to list the ls directory size for the current working directory:

# ls -lah

 ls -lah

As you can see, the output includes the directories and files in the current directory. This is a quick way to check the folder size in the directory listing command. 

How to List Only Folders in the Output

An important use case in the context of this tutorial is to list only the directories in the output. This is a common requirement when you have a large mix of files and folders. 

The ls command offers several options to list this information in the terminal.

# ls -d */ 

This ls command uses the -d flag to list directories instead of listing the contents of the directories. In addition, the */ wildcard ensures that the output matches the “/” in the directories. 

Alternatively, you can pipe the output of the ls command into the grep utility to use the same idea of listing only directories in Linux environments. The syntax of this variation will be as follows:

# ls -F | grep "/$"

Use GUI File Managers to Find Folder Size in Linux

If you have a graphical interface to your Linux system, you can use the installed file manager(s) to get the Linux directory size. 

The good thing about this method is that you can run through these steps and view the size of a specific directory.

  1. Launch the file manager on your system and navigate to the target directory. 
  2. Right click on the folder’s name to bring up the context menu. 
  3. Select Properties (or similar option) to view the properties of the folder.
  4. Usually, the size of the directory is available in the first tab. 

test properties

Conclusion

Knowing how to determine the size of files and folders in Linux is essential for keeping your server running well. In this guide, we discussed du, tree, ls, and ncdu as three simple ways of getting the size of a folder.

Understanding things like readable sizes, sorting, and how folders work helps you manage your computer better. This info lets you keep your computer organized, find big folders, and make smart choices regarding space usage.

FAQs

Q. Are there graphical tools available for visualizing directory sizes in Linux?

Yes, graphical tools like Baobab and QDirStat provide visual representations of directory sizes on Linux.

Q. Can the du command be used in scripts for automation?

The du command is script-friendly and can be integrated into scripts for automating directory size-related tasks.

Q. What command can I use to find the size of a directory in Linux?

The du command is used to get the size of a directory in Linux. For example, du -sh directory_name provides the size in a human-readable format.

Q. Can I find the size of a directory and its subdirectories?

The du command is recursive by default, so running du -sh directory_name includes the sizes of the directory and its subdirectories.

Q. How can I list the sizes of all directories in a particular location?

You can use du -h –max-depth=1 /path/to/parent_directory to list the sizes of all immediate subdirectories in the specified location.

Q. Are there options to sort the directory sizes?

You can use the –max-depth option with the sort command to sort the directory sizes. For example, du -h –max-depth=1 | sort -h.

Q. Can I find the sizes of directories in a remote Linux server?

You can use SSH to access a remote server and run the du command. For instance, ssh user@remote_server ‘du -h directory_name’.

Q. What does the size output in human-readable format mean?

The human-readable format (e.g., KB, MB, GB) in the du command output makes it easier for users to understand the sizes of directories without interpreting raw byte counts.

Q. Can specific directories be excluded from the size calculation?

You can use the –exclude option with du to exclude specific directories from the size calculation. For example, du -h –exclude=exclude_directory.

Q. How often should I check the size of folders on my Linux system?

Checking directories’ sizes is beneficial for managing disk space efficiently. Consider incorporating it into routine system maintenance tasks. Note that you should also consider du hidden files in calculating the disk space occupied by system and user folders.

Narendra

At Redswitches, Narendra works as a DC engineer full-time. He is in charge of Redswitches Asia DC, which is based in India. His area of expertise is server hardware. Rack and DC Operation resolving all OS-related concerns.

Related articles

Latest articles