Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get the total size of all files of a certain type within a directory in linux?

I'm trying to figure out how much space all my JPGs within a particular directory ('Personal Drive') are taking up. I figure there is a command line command to accomplish this.

like image 223
Paul Avatar asked Mar 23 '23 11:03

Paul


2 Answers

You might be able to use the du command ... du -ch *.jpg

like image 114
alex Avatar answered Apr 05 '23 16:04

alex


Found it.

ls -lR | grep .jpg | awk '{sum = sum + $5} END {print sum}'

As I understand it:

  • ls says list all the files in the directory.
  • Adding in the -l flag says show me more details like owner, permissions, and file size.
  • tacking on R to that flag says 'do this recursively'.

  • Piping that to grep .jpg allows only the output from ls that contains '.jpg' to continue on to the next phase.

  • Piping that output to awk '{sum = sum + $5} END {print sum}' says take each line and pull its fifth column element (file size in this case) and add that value to our variable sum. If we reach the end of the list, print that variables value.

like image 31
Paul Avatar answered Apr 05 '23 15:04

Paul