Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to calculate the file size of a directory tree, but I want to ignore all PDFs and images as well. Essentially, I'm trying to figure out what the size of the text-only files are.

How can I do this?

Note: this is in a Linux environment, RedHat in particular

share|improve this question

Consider a directory structure as follows:

$ ls -sR1
total 32
4 1/
4 2/
4 m
4 n
4 o
4 x.pdf
4 y.pdf
4 z.pdf

total 0

total 8
4 a/
4 p

total 4
4 q

Then to get a list of all files which do not match *.pdf, we can do

$ find . -type f \( ! -name '*.pdf' \)

whose expansion to also not match, say, *.jpg is rather obvious. Then use  du as follows:

$ du -c $(find . -type f \( ! -name '*.pdf' \))
4   ./o
4   ./m
4   ./n
4   ./2/a/q
4   ./2/p
20  total

where you might want to throw in a -b to get the apparent size in bytes rather than disk usage. -c gives out the grand total.

The limitations of this approach are:

  1. It won’t work if your directory tree is very large, because
    • Because it scans the tree twice (find + du)
    • Because it feeds the output of find into du via the command line and is thus bounded by the maximum allowed size for that.
  2. It will (in this naive approach) only match files based on their names, not their content.

You can overcome 1. by using find to execute du on each individual file and then use awk or something similar to add it all up. 2. might be solved by more intriguing checks in find, possibly based on the output of file for each file and matching it against a positive or negative list.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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