Linux get all directory sizes

Get Total Size of a Directory in Linux

Introduction

In Linux, ls -l would list the files and directories in a particular path, with their names, dates, and sizes (disk usage). The first thing you’ll notice using that command is that size of directories is always shown as 4096 bytes (or 4,0K if you’re using ls -lh ) even though they contain files that greater than 4 KB in size. The reason is that ls returns meta-data for the directories, not the actual size.

So what’s the shortest and easiest way to get the size of a directory in Linux, you ask? To get the total size of a directory in Linux, you can use the du (disk-usage) command.

In this article, we’ll take a look at some of the most common usages of the du commands, including but not limited to du -sh , du -ch , and du —max-depth .

Getting Size of Directory in Linux with du

To see the full description and argument list of du command, refer to the man du .

If we type du without any arguments, it will list all the directory names and sizes for the current working directory and all sub-directories recursively:

Get Size of the Current Working Directory

To get the size of the current working directory only, and not the sub-directories, we can use du -s or du —summarize :

We can add the -h parameter to get the size in a more human-readable format:

We can also use du with $PATH parameter to get the size of a directory that is located somewhere other than the current working directory:

Note that you would need to use it with sudo privileges for some directories, otherwise you would get a Permission denied error.

Get Size of First-Level Sub-Directories

To get size of first-level sub-directories as well as the total size of the path directory:

-c or —total returns the total size of the path ( 11G total ). * lists all the first-level sub-directories in the /var/ directory. We can also add —exclude to exclude any directory:

Note that excluding lib also affects the total size ( 3,2G total ). This is also equivalent of sudo du /var/ -h —exclude=lib —max-depth=1

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

—max-depth=N will return all sub-directory levels that are equal or less than the number N . Setting —max-depth to 1 returns the first-level, 2 for the second, and so on.

Get Size of All Sub-Directories

To recursively get all subdirectories of /var/ , you can use sudo du /var/ -h . Or you can pass a number to the —max-depth that you’re sure is greater than or equal to the max level of sub-directory depth: sudo du /var/ -h —max-depth=999 .

Читайте также:  Драйвера для ricoh sp 150 windows 10

The second option is more of a workaround rather than the most efficient way.

Get Size of Directory in Linux with tree —du -h

tree is a recursive directory listing program that will list directories and files in a tree-like format. Note that tree is not installed by default. For Debian/Ubuntu, we can install the tree by running sudo apt install tree .

After the installation complete, we use the tree command to list names and sizes of all directories and files in a particular path, in a tree-like format:

Conclusion

In this article, we learned how to get directory sizes in Linux. You can use these commands on Linux remote machines, servers, and/or Linux machines with or without GUI.

For most of the cases du command would be sufficient. It has also the advantage of being installed by default. On the other hand, the tree command would provide a more visual and detailed user interface to display almost the same results, making it a powerful alternative for du .

Источник

How To Find The Size Of A Directory In Linux

This brief tutorial explains how to find the size of a directory in Linux operating systems. Finding the size of files and directories in graphical mode is very easy! All we have to do is just right click on the file or directory, and choose the properties option from the context menu. However, it is equally important to know how to check the size of a directory from CLI mode as well.

Find the size of a directory in Linux

We can get the directory size using ‘du’ command in Linux and Unix-like operating systems. The du command will estimate and summarize file and directory space usage.

For those wondering, du stands for disk usage.

The typical syntax of du command is given below:

Now, allow me to show you how to find directory size in Linux from command line using du command with examples.

1. Display current directory size

Enter ‘du’ command without any options to display the size of the current directory and its sub-directories.

sample output:

Find the size of current directory in Linux

As you see in the above output, du command displays the disk usage of my current directory along with its sub-directories.

2. Display size of a specific directory

To display a particular directory’s size, for example ostechnix, run:

Sample output:

You can also display the size of multiple directories in one go like below:

3. Display directory size in human-readable format

By default, du displays the size in bytes . We can also display the size in «human readable format» (i.e. auto-selecting the appropriate unit for each size), rather than the standard block size.

To do so, add -h tag with du command as shown below.

Sample output:

Now you see the size of the directories in Kilobytes, Megabytes and Gigabytes, which is very clear and easy to understand.

4. Display directory size in a specific format

We can also display the disk usage size only in KB, or MB, or GB.

To do so, use -k for kilobytes, -m for megabytes

5. Display grand total size of directories

We can display just the total human-readable size of the current working directory using -s and -h flags.

Читайте также:  One piece все для windows

Here, -s flag indicates summary.

Sample output:

If you want to check the total disk space used by a particular directory, run:

We can also display the size of multiple directories at once as shown below.

To get the grand total of the combined directories in human-readable format, for example

/Music , add -c flag:

Here, -c refers the cumulative total.

Sample output:

To display only the grand total of the given directory including all the sub-directories, use ‘grep’ command with ‘du’ command like below.

6. Display sizes of a directory and subdirectories, up to N levels deep

A directory may contain large number of sub-directories. You may want to list the sizes of a directory and any subdirectories, only up to N levels deep.

The following command displays the human-readable sizes of the given directory and its sub-directories, up to 2 level deep:

7. Sort directories based on size

To find out which sub-directories consume how much disk size and sort them by their size, use this command:

The largest sub-directories will be displayed on the top. You can increase the directory depth level by increasing the value of —max-depth parameter.

8. Find the size of both files and directories

As you may noticed in the all above outputs, du command only displayed the disk usage of directories. But, what about the files?

To display the disk usage of all items including files and directories, use -a flag.

Now, you will see the disk usage of all files and folders in human readable format.

Sample output:

Display disk usage of files and folders in human readable format in Linux

9. Exclude certian type of files

The following command will display the size of the current directory including its sub-directories, but it will exclude the size of all .mp4 files.

Tip: Can we find the biggest or smallest directories/files? Of course, yes! Check the following guide.

For more details about ‘du’ command, check the man pages.

Suggested Read:

Conclusion

In this guide, we looked at how to find the total size of directory in Linux using du command with examples. As you can see, getting folder or directory size in Linux is not a big deal.

Источник

How to Get the Size of a Directory in Linux

Home » SysAdmin » How to Get the Size of a Directory in Linux

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.

  • 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 du command 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:

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.

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

Читайте также:  Windows 10 отключить защитник windows утилита

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:

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:

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:

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:

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

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 :

If you wanted to list only the top directory and the first layer of subdirectories, change —max-depth=1 :

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

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:

  • For Debian / Ubuntu
  • For CentOS / RedHat

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:

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

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:

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:

  • For Debian / Ubuntu
  • For CentOS / RedHat

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

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:

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

Note: Learn how to move directories in Linux using the GUI or system commands.

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.

Источник

Оцените статью