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{ if (! defined $dq ) { $dq = q{/}; } $dq .= $_ . q{/}; } grep{ m/.+/; } @dp ), qq{\n};