How can I find how much disk space does a list of files use? I'm looking for a variation of
du -s *.sql
I want to see only the grand total, and with the command above, it always shows a line for each file.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityHow can I find how much disk space does a list of files use? I'm looking for a variation of
du -s *.sql
I want to see only the grand total, and with the command above, it always shows a line for each file.
You can use tail
to cut the last line (the total) from the output of du
:
du -sc -- *.sql | tail -n 1
There seems to be no way to make du
itself report just the total of a set of files.
Beware that if any of those *.sql
files are or type directory, then the total will include the disk usage of every file in those directories (recursively). The -s
option is there to reduce the amount of output (which tail
discards anyway) in that case.
With the zsh
shell, you can write:
du -sc -- *.sql(^/) | tail -n 1
To exclude the files of type directory from the glob expansion. The D
glob qualifier can also be added to include hidden files.
For those *.sql
files of type symlink, if you want the disk usage of the target rather than of the symlink itself, you may add the -H
option to du
. Beware that in any case, some 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. With the GNU implementation of du
, the -l
option can be used to skip that 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.
May 23, 2011 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.
-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
.
Dec 22, 2015 at 4:46
-s
decreases the amount of data being written through the pipe (if any of the argument(s) are directories).
Dec 22, 2015 at 4: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
.
May 6 at 0: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).
May 6 at 6:12
cat *.sql | wc -c
Answer is in bytes.