Luky
Luky

Reputation: 67

Ignoring directories with glob

I have some code that parses a directory to get files which hold the hostname of various devices. However I want glob to ignore directories of a certain depth is this possible? So for instance I have

 my @files = glob('/tftpboot/configs/*/*');

my folder architecture looks like

/tftpboot/configs/core/router1 (where router1 is a file)
/tftpboot/configs/test/router2 (where router2 is a file)
/tftpboot/configs/test/firewall/context (context is a file)

I want to get files router1, router2 and any file of a similar depth. However I would like to ignore context any any file of that depth. Any suggestions?

Thank you

Upvotes: 1

Views: 1243

Answers (1)

ikegami
ikegami

Reputation: 385976

Follow up with:

@files = grep { !-d $_ } @files;
  -or-
@files = grep { -f $_ } @files;

You could also use something like File::Find::Rule.

my @files = File::Find::Rule->file->in('/tftpboot/configs');

Upvotes: 3

Related Questions