How to get Directory Size in Linux

Configurare noua (How To)

Situatie

Solutie

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

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

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:

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

# 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

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

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

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

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

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

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

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.

Tip solutie

Permanent

Voteaza

(6 din 17 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?