I have a hundreds of pdf files and html files in a directory. And I want to know total size of pdf files.

By command du -ch /var/foo I can see total file size but I only need last line, the total size.

If the directory contains only pdf files I can use -s option, but the option can't be used this time.

How can I get only total size of particular file type?

  • Cant you use like du -sh /foo/*.pdf ?
    – bagavadhar
    Nov 10, 2014 at 10:56
  • 3
    du -ch /var/foo/*.pdf | tail -n 1 Nov 10, 2014 at 11:42

4 Answers 4


With GNU du (i.e. on non-embedded Linux or Cygwin), you can use the --exclude option to exclude the files you don't want to match.

du -s --exclude='*.html' /var/foo

If you want to positively match *.pdf files, you'll need to use some other method to list the files, and du will at least display one output line per argument, plus a grand total with the option -c. You can call tail to remove all but the last line, or sed to remove the word “total” as well. To enumerate the files in that one directory, use wildcards in the shell.

du -sc /var/foo/*.pdf | tail -n1
du -sc /var/foo/*.pdf | sed -n '$s/\t.*//p'

If you need to traverse files in subdirectories as well, use find, or use a **/ pattern if your shell supports that. For **/, in bash, first run shopt -s extglob, and note that bash versions up to 4.2 will traverse symbolic links to directories; in zsh, this works out of the box.

du -sc /var/foo/**/*.pdf | tail -n1

An added complication with the find version is that if there are too many files, find will run du more than once, to keep under the command line length limit. With the wildcard method, you'll get an error if that happens (“command line length limit exceeded”). The following code assumes that you don't have any matching file name containing a newline.

find /var/foo -name '*.pdf' -exec du -sc {} + |
awk '$2 == "total" {total += $1} END {print total}'
  • (probably not an issue in practice but note that --exclude='*.html' would cause /var/foo/dir.html/foo.pdf not to be counted as the exclusions also affect directory traversal; also, the disk usage of directories including /var/foo itself would be accounted for) Oct 10, 2017 at 13:40

You can let the shell expand the files:

$ mkdir foo
$ echo "abc" > foo/1.pdf
$ echo "abcd" > foo/2.pdf
$ echo "abcd" > foo/3.html
$ du -ch foo/*.pdf
4,0K    foo/1.pdf
4,0K    foo/2.pdf
8,0K    total

However as you can see this indiates filesizes about 1000 times as just created. A better option is using the -b option:

$ du -cbh foo/*.pdf
4   foo/1.pdf
5   foo/2.pdf
9   total

Large filesizes will still be displayed in human readable form—e.g. 173K.

  • du shows the disk usage of each file (on most filesystems, that's the file size rounded up to the next multiple of the block size). With the option -b, du shows the size of each file. Nov 10, 2014 at 22:30

You can use 'find' command to get such result, found in answers to this question:

How to find total filesize grouped by extension


With GNU find and du:

LC_ALL=C find . -iname '*.pdf' -type f -print0 |
  du -ch --files0-from=- |
  tail -n 1

For the cumulative disk usage of all the regular files with names ending in .pdf or any variation in case (.PDF, .pdF...).

For the sum of the size of the files (counting all the hard links of the same files) instead of disk usage, that is how much data could be read from all of them, add the --count-links and --apparent-size options to du, or you can use du -lbch instead of du -ch. (as -b implies --apparent-size). Change -type to -xtype and add the --dereference-args option to du if you also want to count symlinks to PDF files.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .