Linux: Find Out Directory Size Command

Linux: Find Out Directory Size Command

To find out the size of a directory in Linux, you can use the du command. This command stands for "disk usage," and it calculates the size of a directory and all its subdirectories.

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

ref‮al:ot re‬utturi.com
du -sh directory

Replace directory with the name of the directory you want to check. For example, to find the size of the /var/log directory, you would use the following command:

du -sh /var/log

The -s option tells the du command to display the total size of the directory and its subdirectories, and the -h option tells it to display the size in "human-readable" format, meaning it will use units such as kilobytes, megabytes, and gigabytes instead of showing the size in bytes.

You can also use the --max-depth option to specify the maximum depth of the directory tree to include in the calculation. For example, to find the size of the /var/log directory and all its subdirectories, but not the subdirectories of those subdirectories, you can use the following command:

du -sh --max-depth=1 /var/log

This will display the size of the /var/log directory and all its immediate subdirectories, but not the subdirectories of those subdirectories.

You can also use the find command to find the size of a directory. To do this, use the -printf option to print the size of each file and directory in the tree, and pipe the output to the awk command to sum the sizes. For example:

find directory -type f -printf '%s\n' | awk '{total+=$1} END {print total}'

Replace directory with the name of the directory you want to check. For example:

find /var/log -type f -printf '%s\n' | awk '{total+=$1} END {print total}'

This will find all files in the /var/log directory and its subdirectories, print their sizes, and sum them to display the total size of the directory.

Created Time:2017-10-16 14:38:53  Author:lautturi