Shell one-liner to show total size of filetype in directory

find /home/bgstack15 -mtime +2 -name "*.csv" | xargs stat -c "%s" | awk '{Total+=$1} END{print Total/1024/1024}'

This one-liner shows the cumulative size of all the .csv files in /home/bgstack15 (and subdirectories).

The explanation

find /home/bgstack15 -mtime +2 -name “*.csv”
Lists all csv files modified 2 or more days ago in my home directory. If the time of the file is insignificant, just remove the -mtime +2.
Pipe that output to
xargs stat -c “%s”
Some people out there use ls for this, but other people say don’t do that, a la
Anyway, this command takes the standard output from the pipe and adds it to the end of the command, which in this case is stat. Stat here is listing just the file size in bytes for each file. It doesn’t even include the name of the file in this case. That’s all adjustable, of course.
Pipe that output to
awk ‘{Total+=$1} END{print Total/1024/1024}’
This command adds the first delimited (tab, in this case) word from each line and adds it to a variable, “Total.” At the end of all the lines, show the total value divided by 1024 divided by 1024, so the output is in MB (megabytes).


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s