YouTube Icon

Code Playground.

How to Get the Size of a Directory in Linux

CFG

How to Get the Size of a Directory in Linux

When posting the substance of a catalog utilizing the ls order, you may have seen that the size of the registries is quite often 4096 bytes (4 KB). That is the size of room on the circle that is utilized to store the meta-data for the registry, not what it contains. 

The order you'll need to use to get the genuine size of a catalog is du, which is another way to say "circle utilization". 

Getting the Size of a Directory

The du order shows the measure of document space utilized by the predefined records or indexes. In the event that the predetermined way is a catalog, du sums up circle use of every subdirectory in that registry. In the event that no way is indicated, du reports the plate utilization of the current working index . 

When conjured with no choices, du shows the plate utilization of the given index and every one of its subdirectories in bytes. 

Regularly, you would need to show the space involved by the catalog in a comprehensible organization. For instance, to get the complete size of the/var registry, you would run the accompanying order: 

sudo du -sh /var

The yield will look something like this: 

85G	/var

How about we clarify the order and its contentions: 

The order begins with sudo in light of the fact that the vast majority of the documents and indexes inside the/var registry are claimed by the root client and are not meaningful by the ordinary clients. On the off chance that you discard sudo the du order will print "du: can't understand catalog". 

s - Display just the complete size of the predefined index, don't show record size sums for subdirectories. 

h - Print sizes in a comprehensible arrangement (h). 

/var - The way to the index you need to get the size. 

Imagine a scenario in which you need to show the circle utilization of the primary level subdirectories. You have two choices. The first is to utilize the reference mark image (*) as demonstrated as follows, which signifies "coordinate all that doesn't begin with a period (.)". The - c choice advises du to print a terrific absolute, everything being equal: 

sudo du -shc /var/*
24K	/var/db
4.0K	/var/empty
4.0K	/var/games
77G	/var/lib
4.0K	/var/local
0	/var/lock
3.3G	/var/log
0	/var/mail
4.0K	/var/opt
0	/var/run
196K	/var/spool
28K	/var/tmp
85G	total

Another approach to get a report about the plate use of the main level subdirectories is to utilize the - max-profundity alternative: 

sudo du -h --max-depth=1 /var
77G	  /var/lib
24K	  /var/db
4.0K	/var/empty
4.0K	/var/local
4.0K	/var/opt
196K	/var/spool
4.0K	/var/games
3.3G	/var/log
5.0G	/var/cache
28K	/var/tmp
85G	/var
85G	total

As a matter of course, the du order shows the circle space utilized by the catalog or document. To locate the clear size of an index, utilize the - evident size alternative. The "obvious size" of a document is how much information is really in the record. 

sudo du -sh --apparent-size /var

At the point when you move a registry through SCP , Rsync ., or SFTP the measure of information that is moved over the organization is the evident size of the documents. This is the reason the size of room on the plate utilized on the source when shown with du (without - evident size) isn't equivalent to the size on the objective. 

The du order can likewise be joined with different orders with pipes. 

For instance, to print the 5 biggest indexes inside the/var catalog, you would pipe the yield of du to the sort order to sort the registries by their size and afterward pipe the yield to the head order that will print just the main 5 catalogs: 

sudo du -h /var/ | sort -rh | head -5
85G	/var/
77G	/var/lib
75G	/var/lib/libvirt/images
75G	/var/lib/libvirt
5.0G	/var/cache/pacman/pkg

Conclusion

In Linux, you can get the size of an index utilizing the du order. 

In the event that you have any inquiries or comments, leave a remark underneath.




CFG