The number of bytes of all gzip files in the directory

I have a bash script that needs to know the number of bytes for all gzip files in a directory. So far I assume that this is only one directory without subdirectories. It is very tempting to do something like this:

du -scb /my/dir/*.gz|tail -n 1

However, I have TON files. Would *.gzit not expand to any overflow condition? Is there a faster and safer way to check this number?

+3
source share
2 answers

This works and is "safe":

(find . -type f -print0 |
  xargs -0 stat -c '%s' |
  tr '\n' '+'; echo 0) |
  bc

How it works:

  • First use find.gz to search for files. Print them with nul separators, so we can deal with strange file names.
  • xargs . stat -c '%s', ( @Fritschy).
  • tr +. 0 . , " ", bc .
  • bc.
+4
echo $(( $(find . -type f -name '*.gz' -printf '%s+') 0 ))

, '%s+' '%s+0' $(( ... 0 )) ... | bc

. %k %s, . .

bash, , .

+1

Source: https://habr.com/ru/post/1780324/


All Articles