Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl: How do I find a parent folder of a known subfolder?

Tags:

perl

I'm new to Perl and I'm having some trouble getting a parent folder of a known sub-folder(s). So I have a list of subfolders, let's say A, B, C, D, etc. I know they are all scattered around in folders labeled (for eg.), 1, 2, 3, 4, etc.

So it can be like this: 1/A 1/B 2/C 3/D 3/E etc.

I just want to iterate through each folder and find it's direct parent directory. Any ideas?

EDIT: Changed PERL to Perl :P

like image 677
James Goe Avatar asked Mar 11 '23 22:03

James Goe


1 Answers

use File::Find::Rule qw( );

my @matches =
   File::Find::Rule
      ->name(qw( A B C D ))
      ->directory
      ->in('.');

It handles deep dir just fine.

$ mkdir -p a/b/c/d/e/f/g/h/i/j/k/l

$ touch a/b/c/d/e/f/g/h/i/j/k/l/file

$ mkdir -p 0/1/2/3/4/5/6/7/8/9

$ touch 0/1/2/3/4/5/6/7/8/9/file

$ perl -MFile::Find::Rule -E'say for File::Find::Rule->in(".")'
.
a
a/b
a/b/c
a/b/c/d
a/b/c/d/e
a/b/c/d/e/f
a/b/c/d/e/f/g
a/b/c/d/e/f/g/h
a/b/c/d/e/f/g/h/i
a/b/c/d/e/f/g/h/i/j
a/b/c/d/e/f/g/h/i/j/k
a/b/c/d/e/f/g/h/i/j/k/l
a/b/c/d/e/f/g/h/i/j/k/l/file
0
0/1
0/1/2
0/1/2/3
0/1/2/3/4
0/1/2/3/4/5
0/1/2/3/4/5/6
0/1/2/3/4/5/6/7
0/1/2/3/4/5/6/7/8
0/1/2/3/4/5/6/7/8/9
0/1/2/3/4/5/6/7/8/9/file
like image 166
ikegami Avatar answered Mar 16 '23 00:03

ikegami