0

I would like to count overall size of files in some given directory via bash. What's the best approach to do that giben thaht i would be grateful if there was an option to optionaly show result in different units: MBs, GBs.

alop789312
  • 177
  • 1
  • 2
  • 6

2 Answers2

3

du command is what you are looking for.

Type : du -hs folder/*

-s calculate total size in folder
-h makes it human readable

jaypal singh
  • 71,025
  • 22
  • 98
  • 142
UnX
  • 391
  • 1
  • 6
0

For grins and giggles, you can also use ls and awk. With this method, you can choose the units by using multiple divisions of 1024.

# Sum in bytes
ls -1l | grep -v ^total | awk '{sum += $5 } END { print sum "b"}'

# Sum in Kilobytes
ls -1l | grep -v ^total | awk '{sum += $5 } END { print sum/1024 "KB"}'

# Sum in Megabytes
ls -1l | grep -v ^total | awk '{sum += $5 } END { print sum/1024/1024 "MB"}'

# Sum in Gigabytes
ls -1l | grep -v ^total | awk '{sum += $5 } END { print sum/1024/1024/1024 "GB"}'
JamCon
  • 2,293
  • 2
  • 23
  • 34