use strict; use warnings; use File::Spec; my $p = q{/var/www/vhosts/testing.com/httpdocs/test1.html}; my ( $v, $d, $f ) = File::Spec->splitpath( $p ); my @dp = File::Spec->splitdir( $d ); foreach my $i ( 0 .. $#dp ) { next unless ( $i and length $dp[$i] ); print File::Spec->catdir( @dp[0 .. $i ] ), qq{\n}; }