in reply to Is this directory a subdirectory of another directory?

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.
 _  _ _  _  
(_|| | |(_|><
 _|