How to find size of directory in linux

How to find size of directory in linux

Commands to know:

To find out the size of a directory, we will use ‘du’ command. du stands for disk usage.

Syntax:

du  <option>  <filename>
du  <option>  --files0-from=F

for ex see log directory:

du /var/log


you can use du -h to see files size in human readable format

du -k for kilo bytes
du -m for megabytes 
















1. To find which sub-directories consume how much disk size.


 du -h --max-depth=1 | sort -hr
sort -hr to display o/p in descending order

you can change the value of --max-depth=x
x= 0,1,2  etc

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

du -ah /var/log



2. display the size of multiple directories at once as shown below.

du <directory1> <directory2>

3. to check the total disk space used by a particular directory, use the -s flag.
du -sh /var/log

Here, -s flag indicates summary.

4.  for multiple directories summary use

du -sh /var/log  /etc

5. To display the grand total of directories, add -c flag with du -sh command.



du -csh  /var/log  /etc
du -csh /var/log  /




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

du -sch /var/log | grep total



7. You might want to exclude certain 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.

du -ch  --exclude='.*mp4' | grep total

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

man du


Comments

Popular posts from this blog

DOMAIN AND SSL

variables in shell scripting

Usage of find command