15

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.

Kusalananda
  • 333,661
Elazar Leibovich
  • 3,231
  • 5
  • 27
  • 28

6 Answers6

22

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.

Lesmana
  • 27,439
4

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.

jmtd
  • 9,305
2

Your question is very ambiguous but I suspect you are looking for the -c flag to produce a total.

du -c -- *.sql
Caleb
  • 70,105
2

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
totedati
  • 49
  • 2
  • This doesn't add anything to the accepted answer of du -c * | tail -n 1. Also the -s option doesn't do anything here. – Wildcard Dec 22 '15 at 04:44
  • 2
    This is essentially a combination of the other answers.  The -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
  • 1
    @Wildcard: Actually, -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
  • Aha! @G-Man, good catch; you're right. – Wildcard Dec 22 '15 at 05:31
  • 1
    that's why i added -s flag ... my understanding of original query is that is wanted the grand total of all files from subdirectories combined in separate grand totals not the listing for every file each how much space eat. If not then -s is wrong will eat all files from subdirectories. Tail -n 1 is ever better, thanks! – totedati Jul 09 '18 at 08:16
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.

  • (1) This would be a good case for using -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
-4
cat *.sql | wc -c

Answer is in bytes.

jmtd
  • 9,305