Re: Directory level Depth check - Shell script



On Jan 31, 9:26 am, Logan Shaw <lshaw-use...@xxxxxxxxxxxxx> wrote:
Barry Margolin wrote:
In article
<4b83a38d-24ca-486b-999e-8f9977091...@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
Meendar <meen...@xxxxxxxxx> wrote:
Anybody know how to compare the directory depth level of two
directories, whether their depth level are same or not through shell
script. (also depth level for each sub-directory )
i have googled abt this, but couldn't find the prompt answer.
Count the number of '/' characters in the pathnames and compare them.

Of course, for this solution to work in all cases, you've got to
do some sort of canonicalization process first, since "foo/bar"
and "foo/bar/" and "foo/////bar" are all legal ways of referring
to the same directory. :-)

One other possible solution:

dir_depth() {
dir="$1"

depth=1
while [ X"$dir" != X"." -a X"$dir" != X"/" ]
do
dir=`dirname "$dir"`
depth=`expr "$depth" + 1`
done

echo "$depth"
}

It's a little ugly, but it mostly works.

There are some questions to be answered, though: for instance,
what is the depth of the pathname "foo/../foo/../foo/bar"?

- Logan

Thanks to All
.



Relevant Pages