That is, if ./a/b/c/d/e/f is the deepest subdirectory in the output
of the find command,
how can I count those subdirectories in e.g.
find . -type d |
{
while read line ; do
???
done
}
I thought about using sed to extract the slashes but I don't know how
to do the extract
or if that's even the easiest way to do this.
Thanks for your help.
$ x=./a/b/c/d/e/f
$ y=${x//[^\/]}
$ echo $y ${#y}
////// 6
(In this solution you need a modern shell, like ksh93, bash, ..., that
supports ${var//pattern}.)
Janis
> If do a 'find . -type d',
> how can I determine the greatest depth of all the subdirectories in that
> hierarchy?
find . -type d | awk -F'/' '{print NF}' | sort -nu | tail -n1
lg
Or:
find . -type d |
awk -F\/ 'END { print m }
NF > m { m = NF }'
You should move the END block at the end if using old awk.
Regards
Dimitre
Or avoid the sort, if you use awk anyway...
find . -type d | awk -F'/' 'NF>max{max=NF;print max}' | tail -n1
Or avoid sort and tail, if you use awk anyway...
find . -type d | awk -F'/' 'NF>max{max=NF}END{print max}'
Janis
>
> lg
Thank you. This way, my old brain can learn a little bit about awk.
lg
>
> Janis
>
>
>> lg
Thank you. It's mutch better! I dosn't know before.
lg
>
>
> Regards
> Dimitre
Many thanks, Janis and everyone.
I tried the first solution first:
#! /bin/bash -f
deepest=0
find . -type d |
{
while read line ; do
slashes=${line//[^\/]}
count=${#slashes}
if [ "$count" -gt "$deepest" ] ; then
export deepest=$count
export deepest_line=$line
fi
echo $slashes
if [ "$count" -ge 5 ] ; then
echo $line
fi
done
echo ""
echo Deepest: "$deepest"
echo Deepest: "$deepest_line"
}
In my particular hierarchy, the answer is 5 and I have verified
that this is, in fact, correct.
However, why do I get 6 with:
find . -type d | awk -F'/' 'NF>max{max=NF}END{print max}'
and
find . -type d | awk -F'/' 'NF>max{max=NF;print max}' | tail -n1
and
find . -type d |
awk -F\/ 'END { print m }
NF > m { m = NF }'
and
Are these counting the current directory (./) as one of the
subdirectories
whereas the script above is only counting those to the right of the
first slash?
Oh.., yes. This solutions counts also the start-directory,
in this case the "." directory. Here, awk counts the fields between
the slashes, also the field on the leftside of the first slash.
Therefore, ./a/b/c/d/e represents five field-separators (slashes) an six
fields.
You can do:
find . -type d | awk -F'/' 'NF>max{max=NF}END{print (max-1)}'