1

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

1
  • Maybe counting newlines as a character?
    – Bernhard
    Jul 18, 2012 at 7:09

3 Answers 3

2
find . -type d -print | tr -d '\n' | wc -m

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

1

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' {} \; 
1
12
27

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)}'
40
3
  • Btw, you can write just bash -c 'echo -n {} | wc -m'.
    – rush
    Jul 18, 2012 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, 2012 at 8:39
  • 1
    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, 2012 at 12:34
1

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))

You must log in to answer this question.

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