How can I find how much disk space the files in a list use? I'm looking for a variation of
du -s *.sql
I want to see only the grand total, and the command above always shows a line for each file.
How can I find how much disk space the files in a list use? I'm looking for a variation of
du -s *.sql
I want to see only the grand total, and the command above always shows a line for each file.
Use du -c to get the grand total then pipe through tail to get only the last line (the total):
du -c -- *.sql | tail -n 1
There seems to be no way to make du itself report just the total of a set of files.
The -- is not required but highly recommended if this command is to be used in a script. It marks end of options and protects against some funny filenames. See here for more info What does "--" (double-dash) mean?
Some technical nitpicks:
If any *.sql files are "hidden" (filename start with a dot) then they will not be included. This is because by default the glob does not include hidden files.
For *.sql that are directories du will include all files in those directories recursively.
Using zsh you can write *.sql(^/) to exclude directories. Or *.sql(D) to include hidden files. For more info look up zsh glob qualifiers.
For *.sql that are symlinks du will count the size of the link itself instead of the target it points to. Use du -H to get the size of the target of the link.
Beware that in any case most du implementations including GNU du will only count the disk usage of unique files. So if foo.sql is a hard link to bar.sql (or a symlink and -H is used) its disk usage will only be counted once.
This might cause confusion if you plan to copy the files to a filesystem that does not support hardlinks. In most cases the hardlinked files will be copied twice and you will end up needing more disk space on the target system than shown by du.
With the GNU implementation of du the -l option can be used to skip the deduplication.
What doesn't work from your example? Do you want a sum?
man du shows that the -c option provides a sum of usage:
du -sc -- *.sql
You may also like the -h or -k arguments.
Your question is very ambiguous but I suspect you are looking for the -c flag to produce a total.
du -c -- *.sql
Though not standard, with some du implementations, you can add a -h option to get human readable disk usage that use K/M/G/T... suffixes for kibibyte/mebibyte/gibibyte/tebibyte...
du -sch -- * | tail -n 1
If you can generate a list of files (or whatever) using find you can also:
find {directory} {matching expression} -exec stat -c "%s" {} \; | awk 'BEGIN{total=0} {total=total+$1} END{print total/1000000.00}'
For example to see the total size (in MB) of all .jar files in /some/dir:
find /some/dir -name '*.jar' -exec ...
While this does exec a process for each file the result is fairly quick.
cat *.sql | wc -c
Answer is in bytes.