#! /usr/bin/perl use 5.010; use strict; use warnings; use File::Find; my $dir = $ARGV[0] // q{.}; sub deepest { return if not -d; state $prev_dir = ''; say if index $prev_dir, $_; $prev_dir = $_; } find( { wanted => \&deepest, no_chdir => 1, bydepth => 1, }, $dir ); #### a/b/d/g/h a/b/d/g/i a/b/d/g # this is a prefix of the previous, so we don't want it a/b/d a/b/k # this is not a prefix of the previous, so we want it a/b/c/e a/b/c a/b a