Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

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 am trying to use find . -type d -print | wc -m where -m or --chars would print the character counts of the folder names.

Take following screen shot as example, the character count for tempFolder + tempFolder + dingdongFolder is only 34, and event counting the slashes and dots is still 40. Why the command print out 43 characters? Where are the 3 extra characters come from?

enter image description here

share|improve this question
Maybe counting newlines as a character? – Bernhard Jul 18 '12 at 7:09
find . -type d -print | tr -d '\n' | wc -m

tr will delete all newline characters and wc will count number you want.

share|improve this answer

Apparently trailing newlines are counted as a character also.

Suppose you want the character count per directory, you can do

$ find -type d -exec bash -c 'echo -n $0 | wc -m' {} \; 

Which adds up to the 40 that you are looking for. Note that the echo -n removes the trailing newlines.

Summing these three is also possible.

$ find -type d -exec bash -c 'echo -n $0 | wc -m' {} \; | awk '{S+=$1} END  {print(S)}'
share|improve this answer
Btw, you can write just bash -c 'echo -n {} | wc -m'. – rush Jul 18 '12 at 8:31
@rush You're right. For some reason it didn't work when I tried it, but apparently there was something wrong. – Bernhard Jul 18 '12 at 8:39
In a directory which contains 1369 sub directories, this answer was extremely slow. Here is a relative real, user, sys time comparison for 3 answers by: Peter.O : Rush : Bernhard ... real 1.00 : 1.01 : 193.36, user 1.00 : 1.26 : 702.53, sys 1.00 : 1.01 : 109.76... The fastest times (for 100 itterations) were: real 3.699 , user 1.120 , sys 1.120 – Peter.O Jul 18 '12 at 12:34

Set a bash/ksh/zsh array ct to the output of wc -lm, then use shell arithmetic.

ct=($(find . -type d -print |wc -lm)); echo $((ct[1]-ct[0]))

Or use field splitting and positional parameters:

set $(find . -type d -print |wc -lm); echo $(($2 - $1))
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.