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
du -s dir
. Which will summarize disk usage of the directory, and nothing else.
– Elazar Leibovich
May 23 '11 at 13:44
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
du -c * | tail -n 1
. Also the -s
option doesn't do anything here.
– Wildcard
Dec 22 '15 at 04:44
-h
option is not called for; the OP didn't ask for it. | tail -n 1
is better than | grep total
because there might be files whose names contain the word total
.
– G-Man Says 'Reinstate Monica'
Dec 22 '15 at 04:46
-s
decreases the amount of data being written through the pipe (if any of the argument(s) are directories).
– G-Man Says 'Reinstate Monica'
Dec 22 '15 at 04:48
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.
-exec … +
. (2) The user might not appreciate output like “4.2e-05” or “17123.5”. Look at numfmt
.
– G-Man Says 'Reinstate Monica'
May 06 '22 at 00:39
%s
is for size, not disk usage. That implies the GNU implementation of stat
, but if you have GNU stat
, you'll likely have GNU find
which can report that information by itself (with its -printf
which existed decades before GNU coreutils added a stat
command).
– Stéphane Chazelas
May 06 '22 at 06:12
cat *.sql | wc -c
Answer is in bytes.