Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

1 Answer

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

./1:
total 0

./2:
total 8
4 a/
4 p

./2/a:
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
add comment

Your Answer

 
discard

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.