Linux check directory size

Check Directory Size in Linux

In this article, you will learn how to check directory size on Linux using the command line environment. All the commands demonstrated in this article were run on an Ubuntu 20.04 system. All methods and steps are performed in the terminal. You can quickly open the terminal window by typing Ctrl + Alt + t.

Following are some methods you may use to check directory size on Linux systems. We will explore these methods one by one:

Method 1: Check Directory Size Using du Command

The default command used to check the size of the directory is known as the ‘du’ command, which stands for disk usage. The du command is installed on most Linux distributions. Using the du command, you can view the current directory size of your system, as follows:

The above command displays a list of the home directory contents. The numbers displayed to the left show the sizes, in kilobytes, of each object.

Using the -h option, you can also display the output in more descriptive form, as follows:

The above command displays the space in the kilo, mega, and Gigabytes with numbers.

To find the size of the specific directory, use the following syntax:

You will need to run the above command as the sudo user, because some directories require certain permissions access particular directory content.

Use the following command to check the directory size of the /var directory:

With the -hc option, you can display the size of the specific directory in human-readable form, as follows:

You can also change the subdirectory path depth using the max-depth option. For example, if you only wanted to display the top directory, then you would need to set the max-depth=0, as follows:

Similarly, to retrieve the top directory with one layer of subdirectory, then you will set max-depth=1.

If you want to explore more commands related to du, then you can use the following command:

Method 2: Check Directory Size Using tree Command

The tree command is used to display directories, subdirectories, and files in the tree format. You can make this command more useful by inputting flags and options for customization. The tree command does not come already installed on most of the Linux systems. You can install this command using the apt package manager, as follows:

To display the current directory, subdirectories and files use the following command on the terminal:

With the tree command, you may also retrieve the content of a specific directory using the following syntax:

To list the content of /var directory, you will use the following command:

After completing the command, it will display the total number of directories and subdirectories.

To learn more about the tree command, use the following command:

Method 3: Check Directory Size Using ncdu Command

The NCurses Disk Usage, abbreviated ‘ncdu,’ is also used to check the directory size. ncdu is not installed by default on most Linux systems. You will need to install this command using the command line through the apt package manager, as follows:

Читайте также:  Windows storage server 2012 r2 essential

Using ncdu, you can view an interactive display of your system disk usage. Execute the following to try out this command:

The upper top left corner displays the current directory being viewed. The left column displays directory size in the numerical value, where the # signs indicate the size next to each directory. Using the arrow keys, you can navigate between these lines. Here, the purpose of the right arrow is to browse the directory, and the purpose of the left arrow is to move you back.

With the ncdu command, you may also target a particular directory, as follows:

To quit the ncdu interface, press ‘q,’ and for help, press ‘?’.

In this article, you learned how to check directory size of using the terminal command line on Ubuntu 20.04 Linux systems through three different methods. You may explore more commands related to the tree, ncdu, and du commands using the terminal. These commands can be used on all Linux distributions. If you have any questions or suggestions, feel free to comment below.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.

Источник

Checking Directory Size with du Command in Linux

Knowing the size of a file is easy in Linux. All you have to do is to use the -l and -h option with the ls command and it will show you the file size along with file permissions and file timestamps. Here’s a sample output:

You would notice something strange. The ls command shows the size of all the directories as 4 KB. That cannot be correct, can it be? Of course not.

The size of a folder or directory in Linux can be found using the du command. du here stands for disk usage. I’ll explain the logic behind the 4.0K size for the directories later in this tutorial. For the moment, let’s focus on getting the directory size.

If you want to check the directory size in Linux, you can use this command:

This will give you the total size of the said directory in human-readable format, i.e. KB, MB or GB.

Using du command to get directory size in Linux

I am going to show you various examples of the du command that you can use to check the directory size and the disk utilization.

The syntax for the du command is pretty simple.

Let’s see how to use the du command to get the file and directory size information in Linux.

Here’s the structure of the “tutorials” directory I am going to use in this tutorial:

Now if I run the du command in the “tutorials” directory, it will show the sizes of all the subdirectories and then sum the sizes of all the subdirectories and the files at the bottom of it.

This is the output for the tutorials directory.

Show disk size in human-readable format

Now the problem with the above output of the du command is that you don’t know if the 100 is 100KB or 100MB or 100GB. Don’t worry, you can change this behavior and display the directory size in a human readable format with the option -h.

Here’s the much easier to read output now:

You can use -m option for MB and -k option for KB instead of -h. But even if the size is less than 1 MB, it will always show the size as 1MB. This is why using -h option is always a better option.

Show the sizes of the files as well

Did you notice that the tutorials directory has several files but they don’t show up in the du command output? It’s because though the file size are counted in the total sum of the directory size, the files are not displayed by default.

Читайте также:  Batman arkham city нет windows live

To display the size of files along with the directories, you can use the -a option. It would be better if you combine it with -h option to get the sizes in human readable format.

Now the output will show the files along with the directories:

Bonus Tip: Solving the mystery of 4 KB

Note that the sizes may seem like that have been rounded off. I mean all the sizes are in the multiple of 4K. In fact, apart from the empty files, all the files are of at least 4 KB in size. Is that a coincidence? Not really.

Even if the text of the file is in bytes, the minimum file size is 4K because that’s the minimum block size of the filesystem. Irrespective of the size of the text on the file, it will be allotted at least one block of 4KB memory on the disk.

And since the memory blocks are 4KB in size, the sizes you’ll see will always be in the multiple of 4KB.

Now, you probably already know that everything is file in UNIX/Linux. A directory is essentially a file that has the information about all the location of all the files it ‘contains’.

So, when you use the ls command, it treats the directory as a file and shows its size which is one memory block and thus the size displayed is 4KB.

Show only the total size of the directory in Linux

If you find the output of the du command too verbose and would like to see just the total size of the directory in a human readable format, you can use the sum option -s.

Now the output will be just one line showing the total size of the directory:

Show the disk usage by multiple directories

It’s not that you are restricted to check the size of only one directory at a time. You can specify multiple directories in the du command.

For example, I am going to use the -sh options to show the total size of two directories here.

The output will show the size of both the directories individually:

Show the grand total of all the directory sizes

In the above example, you saw the total sizes of both the directories individually. You can use the option -c to show a grand total for the sum of all the directories in the output.

As you can see in the output, it sums up the sizes and gives you the grand total:

Don’t show the sizes of the subdirectories

What if you want to check the sizes of the all the directories in the current folder? By default, du command will go into the subdirectories of all the directories and the output becomes difficult to figure out specially if you have too many nested directories.

What you can do is to define the depth level to check while showing the sizes for the subdirectories.

So, if you want to see the sizes of the directories in the current folder, you can set the depth to 1 like this:

Now the output will show the subdirectories only in the current directory. It won’t go further than this.

If you cannot remember the –max-depth, you can use its short form -d flag:

Exclude certain type of files while calculating disk size

The du command gives you option to exclude certain type of files. You can use regex along with the –exclude option.

For example, to calculate the sum of all the files excluding files with extension txt, this command can be used:

And now if you see the output, the total size of the directory would have been reduced:

Bonus Tip: Finding the biggest subdirectory

You can combine the output of the du command with the sort command to sort the directories by the order of their size.

Читайте также:  Создание загрузочного iso mac os

This will show the directories in the reverse order of their size i.e. the biggest directory on the top.

Of course the top one is the directory itself but the second one gives you the biggest subdirectory.

You can further combine with the head command or the tail command to get the x largest file or smallest files/directories.

Did you find it useful?

I have tried to explain all the essential usage of the du command in Linux. But as always, there are many more options available for the command that you can find in its man page. If you just wanted to find out the size of a directory in Linux, this tutorial should give you enough information. If you want to check disk space in Linux, use the df command.

Did you like the tutorial? Did it help you? Let me know in the comments. If you have some other cool tip about du command, why not share it with us?

Источник

How to find the size of a directory in Linux

It is very easy to check the size of directories and files in Linux using the GUI, but it is not always easy to get the size of a directory using the command line.

The ls command can be used to list the contents of a directory, but it does not display the exact directory size and always shows each directory size as 4096 bytes (4 KB), which is the size of space on the disk that is used to store the meta-information for the directory, not what it contains

You can get the actual size of a directory using the du command (Disk Usage), which is widely used by Linux administrators, but you can explore other commands for this purpose. Let’s explore them.

This guide shows you how to find the directory size in Linux using the below three commands:

  • du command
  • ncdu command
  • tree command

Method-1: Get the size of a directory in Linux with du command

The du command refers to disk usage. It is a standard Unix program that is used to estimate disk space usage in the present working directory when no path is specified.

It recursively summarizes the disk usage to obtain a directory and its sub-directory sizes.

Use the below du command format to get the total size of each directory, including sub-directories.

The above command will print the size of each file and the actual size of each directory, including their sub-directory as well as the total size.

Details:

  • du: It’s a command
  • -h: Print sizes in human readable format (e.g., 1K, 234M, 2G)
  • -c: Produce a grand total
  • /home/daygeek/Documents/: The path of directory
  • sort -rh: Sort the results with numerical value
  • head -20: Output the first 20 lines result

Use the following du command format to get the total size of a specific directory:

If you want to find out the size of the first-level sub-directories, including their sub-directories, for a given directory on Linux, use the following du command format:

Method-2: Find the size of a directory in Linux with ncdu command

The ncdu (NCurses Disk Usage) is a curses-based version of the well-known ‘du’ command, and provides a fast way to see which directories are consuming your disk space.

The ncdu command scans the given directory and displays their files and folder sizes recursively as shown below:

Method-3: Check a directory size in Linux with tree command

The pstree command displays directory contents recursively in a tree-like format, which is very convenient way to display the directory hierarchy and that improves the readability of the output.

As you can see, the tree command output is straight forward compared to the du & ncdu commands.

Conclusion

You have learnt the three different commands to find the size of a directory in Linux.

If you have questions, feel free to leave a comment below, and we will get back to you as soon as we can. Happy learning!

Источник

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