Home > Archive > Unix Programming > January 2008 > Directory level Depth check - Shell script
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Directory level Depth check - Shell script
|
|
| Meendar 2008-01-29, 8:27 am |
| Hi,
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.
Thanks in Advance
| |
| Chris F.A. Johnson 2008-01-29, 7:26 pm |
| On 2008-01-29, Meendar 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 )
path=/absolute/path/to/directory
IFS=/
set -f
set -- $path
depth=$#
--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
| |
| Barry Margolin 2008-01-30, 4:34 am |
| In article
<4b83a38d-24ca-486b-999e-8f99770911a8@u10g2000prn.googlegroups.com>,
Meendar <meendar@gmail.com> wrote:
> Hi,
>
> 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.
>
> Thanks in Advance
Count the number of '/' characters in the pathnames and compare them.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
| |
| Logan Shaw 2008-01-30, 10:24 pm |
| Barry Margolin wrote:
> In article
> <4b83a38d-24ca-486b-999e-8f99770911a8@u10g2000prn.googlegroups.com>,
> Meendar <meendar@gmail.com> wrote:
[color=darkred]
> 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
| |
| Meendar 2008-01-31, 7:27 pm |
| On Jan 31, 9:26 am, Logan Shaw <lshaw-use...@austin.rr.com> wrote:
> Barry Margolin wrote:
>
> 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
| |
| Scott Lurndal 2008-01-31, 7:27 pm |
| Barry Margolin <barmar@alum.mit.edu> writes:
>In article
><4b83a38d-24ca-486b-999e-8f99770911a8@u10g2000prn.googlegroups.com>,
> Meendar <meendar@gmail.com> wrote:
>
>
>Count the number of '/' characters in the pathnames and compare them.
>
What would you consider the depth of this name:
/tmp/fred//////joe/sam
4 or 9?
scott
|
|
|
|
|