12

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.

6 Answers 6

18

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.

3

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.

1
  • See my clarification. I want it to print only the sum. May 23, 2011 at 13:41
2

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

du -c -- *.sql
1
  • I want it to have the same effect as du -s dir. Which will summarize disk usage of the directory, and nothing else. May 23, 2011 at 13:44
1

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
5
  • 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, 2015 at 4: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. Dec 22, 2015 at 4:46
  • 1
    @Wildcard: Actually, -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
  • Aha! @G-Man, good catch; you're right.
    – Wildcard
    Dec 22, 2015 at 5:31
  • 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 9, 2018 at 8:16
0

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.

2
  • (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. 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
-4
cat *.sql | wc -c

Answer is in bytes.

1
  • 5
    Yeah, but you need to read the whole whopping 10Gb in order to tell it... May 26, 2011 at 4:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.