$str = "/dir1/dir2/dir3/file.txt"; $str =~ s/(.*)\/[^\/]*$//; print "Result: '$1'";