Perl: List files and directories recursively but exclude some directories and files that passed

311 views Asked by At

Please give any suggestion or snippet or anything that may work.

I have already tried wanted function but how do I exclude some directory while recursing?

1

There are 1 answers

1
csay On

In Linux, you can make use of the Linux "find" and "grep" commands and run those Linux commands in Perl using qx to store Linux command result in Perl.

e.g.

$cmd = "find . | grep -v 'dir1\|dir2\|...\|dirn'";
$result=qx($cmd);

The above command combinations do the following:

  1. The find command will list the all the directory and files recursively.
  2. The pipe "|" will pass the find result to grep command
  3. The grep -v command will print on screen only the string not exist in the "dir1", "dir2"..."dirn" to be ignored
  4. At last, the qx command will execute the find and grep Linux commands and stored the output to $result variable.

You can do the similar thing in Windows. The only difference is to use the Windows command line.

e.g.

$result=qx('dir /b/s | find /v "workspace" | find /v "TVM"')

The above command will list all the directory recursively except the directory has name "workspace" or "TVM".