crazyinsomniac gave the quickest solution, IMO.
However, since Ryszard provided the theory, here is a bit of practice. :)
Someone would say that it is overkill, but knowing this method might be useful in a broader context.
For this exercise, I created a "testtree" directory, with some subdirs as you can see at the end of the script. You can either recreate this tree or provide real names.
#!/usr/bin/perl -w use strict; use File::Find; use Tree::DAG_Node; my $count =0; my $rootname="/home/perl/"; # or your favourite dir my $dirname= "testtree"; my $root = new Tree::DAG_Node; $root->name($dirname); my $node = $root; find(\&{ sub { if (-d "$File::Find::dir/$_"){ my $dir = $File::Find::dir; $dir .= "/$_" if $count++; $dir =~ s/^$rootname//; my $ancestor = $node; while ($ancestor) { my $name = $ancestor->name; if ((length($dir) > length($name)) && $dir =~ /^$name/) +{ my $nnode = new Tree::DAG_Node; $ancestor->add_daughter($nnode); $nnode->name($dir); $node = $nnode; last; } $ancestor = $ancestor->mother; } } } }, "$rootname$dirname"); sub is_below { my $node = shift; my $dir = shift; return scalar grep {$dir eq $_->name} $node->ancestors; } sub is_above { my $node = shift; my $dir = shift; return scalar grep {$dir eq $_->name} $node->self_and_descendants; } sub find_node { my $node = shift; my $dir = shift; my ($descendant) = grep {$dir eq $_->name} $node->self_and_descendants; return $descendant; } my $source = "testtree/a/k"; my $dest = "testtree/a"; my $n = find_node($root,$source) or die "can't find dir\n"; if (is_below($n,$dest)) { print "[$source] is below [$dest]\n"; } elsif (is_above($n,$dest)) { print "[$source] is above [$dest]\n"; } print "\nthe tree\n\n"; print $root->dump_names,"\n"; print "the same, fancier\n"; print map "$_\n", @{$root->draw_ascii_tree}; __END__ output: [testtree/a/k] is below [testtree/a] the tree 'testtree' 'testtree/b' 'testtree/b/y' 'testtree/b/x' 'testtree/a' 'testtree/a/k' 'testtree/a/k/m' 'testtree/a/k/l' 'testtree/a/j' the same, fancier (simplified, for posting purposes) | <testtree> /-----------------------\ | | <b> <a> /---------\ /-------\ | | | | <b/y> <b/x> <a/k> <a/j> /--------\ | | <a/k/m> <a/k/l>
This script will build a directory tree using Tree::DAG_Node.
Using standard methods, you can find out if a node is below or above another one. is_above and is_below are shortcuts to achieve our purposes through Tree::DAG_Node methods.

update (1)
This script is not CGI safe.
If you need to use it in a CGI environment, some sanitizing will be necessary.
update (2)
Adjusted the code to shorten it.
 _  _ _  _  
(_|| | |(_|><
 _|   

In reply to Re: Is this directory a subdirectory of another directory? by gmax
in thread Is this directory a subdirectory of another directory? by mt2k

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.