Hello!
Yesterday I tried to make a shell script in order to find the number of directories in my home directory.
I tried with this:
c=0
for dic in $(find ~ -type d); do
((c++))
done
echo $c
The result is 4071.
If I enter " find ~ -type d | wc -l
" code, the result is different (3928).
Can anyone help me?:wall:
RudiC
June 7, 2018, 2:58am
2
Welcome to the forum.
Looks like you have directory names with spaces in them. The for
construct will split its input there and regard those fragments as extra items, increasing the c
count artificially.
2 Likes
You can set IFS to a newline character, then the for list splits only on newline
oIFS=$IFS
IFS="
"
for dic in $(find ~ -type d); do
((c++))
done
# restore the old behavior
IFS=$oIFS
Alternatively you can pipe to "wc -l" or "grep -c ."
c=$(find ~ -type d | wc -l)
1 Like
Thank you...
It is working well now.