kevictor83 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I'm having trouble implementing the following non-recursive find in perl-

find ./files_36/ \( ! -name files_36 -prune \) -name "*fiPL.csv*" -print

The above works from the command line. The following code below in Perl doesnt work though.

my $patt = "*".$_."*"; my $lc_file1 = ""; my $cmd = "find $dir1 \(! -name /proj/dev_dbdump/files_36 -prune\) -n +ame $patt -print"; $lc_file1 = `$cmd`;

I've tried doing a few things, such as removing the escape characters, but all i get is the following compile error!- sh: syntax error at line 1: `(' unexpected I would appreciate any advise!!! NOTE: Due to the nature of the input I'm getting, I'd rather use unix find, as opposed to reg ex.

Replies are listed 'Best First'.
Re: Backtick Query
by chrestomanci (Priest) on Mar 06, 2011 at 10:35 UTC

    When you define $cmd as a string literal, you need to escape the backslashes, so you need to write:

    my $cmd = "find $dir1 \\(! -name /proj/dev_dbdump/files_36 -prune\\) +-name $patt -print";

    You many also need to escape the search pattern, depending on what it is

    Having said that, I think you would be better off investigating File::Find

Re: Backtick Query
by roboticus (Chancellor) on Mar 06, 2011 at 16:20 UTC

    kevictor83:

    First, I'd suggest you use File::Find instead of invoking find through the shell. Next, there's a nice tool that comes with File::Find that will translate the find command into the equivalent perl code, which you can then modify to do what you like, like so:

    $ find2perl ./bin \( ! -name files_36 -prune \) -name "*fiPL.csv*" -pr +int #! /usr/bin/perl -w eval 'exec /usr/bin/perl -S $0 ${1+"$@"}' if 0; #$running_under_some_shell use strict; use File::Find (); # Set the variable $File::Find::dont_use_nlink if you're using AFS, # since AFS cheats. # for the convenience of &wanted calls, including -eval statements: use vars qw/*name *dir *prune/; *name = *File::Find::name; *dir = *File::Find::dir; *prune = *File::Find::prune; sub wanted; # Traverse desired filesystems File::Find::find({wanted => \&wanted}, './bin'); exit; sub wanted { my ($dev,$ino,$mode,$nlink,$uid,$gid); ( (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) && ! /^files_36\z/s && ($File::Find::prune = 1) ) && /^.*fiPL\.csv.*\z/s && print("$name\n"); }

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Backtick Query
by toolic (Bishop) on Mar 06, 2011 at 13:37 UTC