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 http://mywiki.wooledge.org/ParsingLs.
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).

Advertisements