determine the greatest depth of subdirectories in a hierarchy

9 views
Skip to first unread message

luxor...@gmail.com

unread,
Oct 30, 2008, 4:57:25 PM10/30/08
to
If do a 'find . -type d',
how can I determine the greatest depth of all the subdirectories in
that hierarchy?

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.

Janis Papanagnou

unread,
Oct 30, 2008, 5:36:21 PM10/30/08
to

$ 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

Laurianne Gardeux

unread,
Oct 30, 2008, 5:40:31 PM10/30/08
to
Le Thu, 30 Oct 2008 13:57:25 -0700, luxor1275bc a écrit :

> 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

Radoulov, Dimitre

unread,
Oct 30, 2008, 6:00:06 PM10/30/08
to

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

Janis Papanagnou

unread,
Oct 30, 2008, 6:02:21 PM10/30/08
to

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

Laurianne Gardeux

unread,
Oct 30, 2008, 7:06:31 PM10/30/08
to

Thank you. This way, my old brain can learn a little bit about awk.

lg

>
> Janis
>
>
>> lg

Laurianne Gardeux

unread,
Oct 30, 2008, 7:06:14 PM10/30/08
to

Thank you. It's mutch better! I dosn't know before.

lg

>
>
> Regards
> Dimitre

luxor...@gmail.com

unread,
Oct 30, 2008, 7:35:29 PM10/30/08
to
On Oct 30, 3:02 pm, Janis Papanagnou <janis_papanag...@hotmail.com>
wrote:

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

luxor...@gmail.com

unread,
Oct 30, 2008, 8:54:09 PM10/30/08
to

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?

Laurianne Gardeux

unread,
Oct 30, 2008, 8:47:21 PM10/30/08
to

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

Javi Barroso

unread,
Oct 31, 2008, 9:35:30 PM10/31/08
to
On Oct 31, 1:47 am, Laurianne Gardeux <l...@romandie.com> wrote:
> Le Thu, 30 Oct 2008 16:35:29 -0700, luxor1275bc a écrit :
>
>
>
> > On Oct 30, 3:02 pm, Janis Papanagnou <janis_papanag...@hotmail.com>
> > wrote:
> >> Laurianne Gardeux wrote:
> >> > Le Thu, 30 Oct 2008 13:57:25 -0700, luxor1275bc a écrit :
>
> >> >>If do a 'find . -type d',
> >> >>how can I determine the greatest depth of all the subdirectories in
> >> >>that hierarchy?
Another solution (I don't know how portable is it):
find . -type d -printf "%d\n" | sort -n | tail -1


Reply all
Reply to author
Forward
0 new messages