Gm to all, I need to find the sum of the total files of a certain type (for example pdf) in my entire linux machine (total file size, not the total occupied size). For example i want to know the total size of all my pdf files or my jpg files starting from the root. Of course, i need to use bash. Any suggestion? (the only thing that i've learnt is that i should use -b option if using du).

Many thanks!

  • You might want to take a look at the find command: you can pass it a call to du with its -exec arg – joH1 Jan 16 '18 at 14:17

With GNU find:

(echo 0; find / -name '*.pdf' -printf '%s+\n'; echo p) | dc

That gives a sum of the file size (not disk usage) of all the regular (excluding symlinks, directories, fifos...) pdf files.

find / -name '*.pdf' -print0 | xargs -0 du -skc
  • 1
    That only works if the list of file is small enough that only one du invocation is needed. – Stéphane Chazelas Jan 16 '18 at 14:13
  • For pdf files of type directory, that would count the disk usage of the file itself and also all the files under it. – Stéphane Chazelas Jan 16 '18 at 14:14
  • Note that du reports disk usage, not file size. GNU du has a --apparent-size for file size. – Stéphane Chazelas Jan 16 '18 at 14:15
  • Note that for files that have several links, that counts only one of them. With GNU du, you can disable that with -l – Stéphane Chazelas Jan 16 '18 at 14:15
  • 1
    Note that if there's no pdf file, that will give you the disk usage of the current directory (GNU xargs has a -r option to avoid that, or you can use the standard -exec du... {} + instead of the -print0/xargs -0 GNU extension) – Stéphane Chazelas Jan 16 '18 at 14:17

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.