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.
Asked
Active
Viewed 347 times
0
-
Just the files in directory itself or do you need to count all files in all subdirectories of that directory? – Digital Trauma Apr 03 '14 at 23:29
2 Answers
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
- 74,723
- 23
- 102
- 147

UnX
- 421
- 3
- 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,313
- 2
- 25
- 34