foreach my $path ("/a/b/c/d/../../../e" , "/a/../b/./c//d") { my @c= reverse split m@/@, $path; my @c_new; while (@c) { my $component= shift @c; next unless length($component); if ($component eq ".") { next; } if ($component eq "..") { my $i=0; while ($c[$i] =~ m/^\.{0,2}$/) { $i++ } splice(@c, $i, 1); next } push @c_new, $component; } print "/".join("/", reverse @c_new) ."\n"; }