On Tue, Jul 29, 2008 at 2:52 PM, Puneet Lakhina
<[EMAIL PROTECTED]> wrote:
> On Tue, Jul 29, 2008 at 12:28, Anupam Jain <[EMAIL PROTECTED]> wrote:
>
>> On Tue, Jul 29, 2008 at 12:11 PM, आशीष शुक्ल Ashish Shukla
>> <[EMAIL PROTECTED]> wrote:
>> > -----BEGIN PGP SIGNED MESSAGE-----
>> > Hash: SHA1
>> >
>> > In <[EMAIL PROTECTED]>, Anupam
>> Jain wrote:
>> >>Hi all,
>> >>
>> >>Does anyone know of a way to get the maximum directory nesting,
>> >>starting from the specified base, using standard Linux commands only
>> >>(no php/perl/python etc.)?
>> >>
>> >>e.g. if the directory structure is as follows -
>> >>d1
>> >>->d2
>> >>->->d3
>> >>->->->d4
>> >>->d5
>> >>->->d6
>> >>
>> >>I need a shell script which will give me the answer "4" (i.e. depth of
>> >>the deepest directory - d4).
>> >
>> > [EMAIL PROTECTED] ~ % find . -type d -printf '%d\n' |sort -n |tail -1
>>
>> Thanks for the quick replies Gora and Ashish!
>>
>> Actually even though I said bash, I am running this on busybox which
>> does not support -printf for the find command. But I drew enough
>> inspiration from your solution to come up with -
>>
>> find -type d | awk -F'/' '{print NF-1 "\n"}' | sort -n | tail -1
>
>
> This wont well with absolute directory names.
> for e.g. if you use
> find /home/username -type d | awk -F'/' '{print NF-1 "\n"}' | sort -n | tail
> -1
>
> then it will add 2 (corresponding to /home/username) to the max dpeth. This
> is because the paths returned from the find command are of the sort
>
> /home/username/dird1 (depth as per awk expression 3. Actual depth relative
> to base path 1)
> /home/username/dir1/dird2 (depth as per awk expression 4 Actual depth
> relative to base path 2)
>
> You should use something like following if you dont have printf
>
> find /home/user -type d | sed 's/^\/home\/user//g' | awk -F'/' '{print NF-1
> "\n"}' | sort -n | tail -1
>
> essentially remove out the base search path from the find result strings.

Interesting! Now the question is - is there a way to *painlessly*
specify the directory argument (/home/user in this case) only *once*
and get the same result? Even if I put this one liner in a shell
script and refer to $1 in both places, it seems I will still need to
escape slashes before passing it to sed and another sed expression
would make this nice one liner a bit clutzy.

-- Anupam
_______________________________________________
ilugd mailinglist -- ilugd@lists.linux-delhi.org
http://frodo.hserus.net/mailman/listinfo/ilugd
Archives at: http://news.gmane.org/gmane.user-groups.linux.delhi 
http://www.mail-archive.com/ilugd@lists.linux-delhi.org/

Reply via email to