Bash – Check folder size in Bash

bashdirectoryshell

I'm trying to write a script that will calculate a directory size and if the size is less than 10GB, and greater then 2GB do some action. Where do I need to mention my folder name?

# 10GB
SIZE="1074747474"

# check the current size
CHECK="`du /data/sflow_log/`"
if [ "$CHECK" -gt "$SIZE" ]; then
  echo "DONE"
fi

Best Answer

You can do:

du -hs your_directory

which will give you a brief output of the size of your target directory. Using a wildcard like * can select multiple directories.

If you want a full listing of sizes for all files and sub-directories inside your target, you can do:

du -h your_directory

Tips:

  • Add the argument -c to see a Total line at the end. Example: du -hcs or du -hc.

  • Remove the argument -h to see the sizes in exact KiB instead of human-readable MiB or GiB formats. Example: du -s or du -cs.