Paul
Paul

Reputation: 1242

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.

Upvotes: 6

Views: 2013

Answers (2)

alex
alex

Reputation: 806

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

Upvotes: 6

Paul
Paul

Reputation: 1242

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.

Upvotes: 7

Related Questions