#!/usr/bin/env perl ## this should correctly sort directory/file tree ## files first, then dirs use warnings; use strict; use File::Find; sub build_flist { my($fpath); our(@flist, @dlist, @overall); $fpath = shift; @flist = ""; @dlist = ""; @overall = ""; find(\&wanted, "$fpath"); ## two arrays, first is for directories ## second contains files only sub wanted { my $found = $File::Find::name; push(@dlist, $found) if(-d "$found"); push(@flist, $found) if(-f "$found"); }; # because we'll pop() them out, we need to sort them reversely @dlist = sort {$b cmp $a} @dlist; @flist = sort {$b cmp $a} @flist; ## my idea is, to have a cycle where each element of dirs array (@dlist) will be pop()ed ## from @dlist (thats why we sorted them reversely and why we use for(1 .. @dlist)) and ## pushed into @overall, when done .. for(1 .. @dlist) { my $foo = pop(@dlist); push(@overall, "$foo") if($foo ne $overall[@overall - 1]); my $cdir = $overall[@overall - 1]; ## here should be second cycle, which pop()s and push()es elements from ## @flist into @overall and stops, when current directory represent by $dir ## is filled up. ## this check is done by fact, that for sorted @flist it stops pop()ing while ## `$cdir ne "last element of file cut by filename"` .. }; return @dlist; }; my $action = $ARGV[0]; print "$_\n" foreach(build_flist("$action")); exit;