+5 votes
180 views
in Linux/Unix by (242k points)
reopened
View Linux directory size command

1 Answer

+3 votes
by (1.6m points)
 
Best answer

How to see the size of a directory in Linux with du

Managing Linux systems involves many tasks, some more important than others, and one of them is precisely having a clear awareness of the size of the directories where we host the files and variables, this is key to knowing the size they occupy on the disk and with it determine debugging tasks in case we have excess unusable space on the computer..

 

 

We could go file by file to see their properties and with this determine the size of each one, but this logically becomes a tedious and extensive task, to prevent this Linux offers us the du command for a much more complete control.

 

Du is a standard Linux command with which we access details and information about disk usage in an integral way, du works integrally for specific directories and has variations that allow us to customize the output according to the information requirements..

 

getFastAnswer will explain how to use the du command to see the size of a directory in Linux.

 

 

To stay up to date, remember to subscribe to our YouTube channel!   SUBSCRIBE

 

 

How to see the size of a directory in Linux with du

 

Step 1

The basic syntax is to run du without any parameters, as a result we will see the following:
 du 
image

 

 

The values ​​that we see on the far left are the disk usage, then we see the specific directory and at the end of the result we find a summary of the entire / home directory..

 

Step 2

It is possible to use du for a specific directory:
 du directory 
image

 

Step 3

We see that the result is in kilobytes, we can display the size in "human readable format" with the -h parameter:
 du directory -h 
image

 

Step 4

This result is now displayed in MB, this value can be expressed in megabytes or kilobytes as necessary in the following way:
 du -k directory / (kilobytes) du -m directory / (megs) 
image

 

Step 5

When using the du command, we will see the largest subdirectories at the top, to increase the depth level of the directory, we are going to use the --max-depth parameter as follows:
 du -h --max-depth = 1 | sort -hr 
image

 

Step 6

In case we want to display the disk usage of all elements, including files and directories, we will use the -a parameter:
 du -ah / directory 
image

 

Step 7

With the du command it is possible to display two or more directories at the same time, for this we execute the following syntax.
 du Directory 1 Directory 2 
image

 

Step 8

To check the total usage of used disk space for a single directory, we'll use the -s parameter:
 du -sh / directory 
image

 

Step 9

This applies to global directories:

 

 

image

 

Step 10

The du command allows us to display global totals thanks to the -c parameter as follows:
 du -csh directory 
image

 

Step 11

We can only display the general total of the directory including all subdirectories, for this we must use the grep command with the du command like this:
 du -ch Downloads / | total grep 
image

 

Step 12

The general parameters of the du command are:

 

End each line of output with NULL
 -0, –null 

Writes the count of all files, not just directories
 -a, –all 

Print apparent sizes, rather than actual disk usage
 –Apparent-size 

Scale sizes to SIZE before printing to console
 -B, –block-size = SIZE 

Generates the grand total of the directory size
 -c, –total 

Print the total for the directory only if it is N or less levels that are less than the command line argument
 d, –max-depth = N 

Print the result in human-readable format
 -h, –human-readable 

Applies to directories, in this case it does not include the size of the subdirectories
 -S, -separate-dirs 

Shows only the total of each directory
 -s, –summarize 

 

Displays the time of the last modification of any file or directory
 –Time 

 

With the du command we have at hand a comprehensive solution to know the size of a directory in Linux.

 


...