I'm trying to calculate the total size in bytes of all files (in a directory tree) matching a filename pattern just using the shell. This is what I have so far:
find -name *.undo -exec stat -c%s {} \; | awk '{total += $1} END {print total}'
Is there an easier way to do this? I feel like there should be a simple du or find switch that does this for me but I can't find one.
To be clear I want to total files matching a pattern anywhere under a directory tree which means
du -bs *.undo
won't work because it only matches the files in the current directory.
Put together from gerdemb's and strager's contributions. Using
du -cb
should display bytes.Check the du (disk usage) command.
With zsh, you can use extended globbing to do:
du -c **/*.undo
How about this simple one.
I use du command like this to get the number only:
Or, you can just do this:
Where stat displays a file or file system status. The argument -c means using the specified format instead of the default one, and the format sequence $s allows the display of the total size of bytes.
Just evaluates an expression.