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 ); print join( qq{\n}, map{ File::Spec->catdir( @dp[0 .. $_] ) } grep{ length $dp[$_]; } 0 .. $#dp ), qq{\n};