Friday, 15 April 2016

How to find size of a directory ?


Best command to find size of a directory (including sub-directory)
$ du -ch | grep total


'du' - Finding the size of a directory

$ du
Gives a list of directories that exist in the current directory along with their sizes.
Last line of the output gives you the total size of the current directory including its sub-directories.

$ du /home/shaan
Gives directory size of the directory /home/shaan

$ du -h
Option '-h' stands for human readable format.
The sizes of the files / directories are this time suffixed with a 'k' if its kilobytes and 'M' if its Megabytes and 'G' if its Gigabytes.

$ du -ah
'-a' displays the file names along with the directory names in the output.
'-h' is once again human readable format.

$ du -c
Gives the grand total as the last line of the output.
Example: If your directory occupies 30MB the last 2 lines of the output would be :
30M .
30M total

The first line would be the default last line of the 'du' output indicating the total size of the directory and another line displaying the same size, followed by the string 'total'.

$ du -ch | grep total
Displays only one line in its output that displays the total size of the current directory including all the sub-directories.

$ du -s
Displays a summary of the directory size.
(Simplest way to know the total size of the current directory)

$ du -S
Displays the size of the current directory excluding the size of the sub-directories that exist within that directory. It basically shows you the total size of all the files that exist in the current directory.

$ du --exculde=xls
Displays the size of the current directory along with all its sub-directories, but it would exclude all the files having the given pattern present in their file names.

For the files matched by the pattern (within the current directory or any of its sub-directories) their size would not be included while calculating the total directory size.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.