shell - How to use the Unix/AIX find command with a pipe in Perl? -


i'm trying use unix/aix find command piped head command return first file in directory , assign variable. however, of attempts have resulted in files find returns being assigned variable without head command being applied.

here's 3 attempts:

attempt 1:      $first_file = `/usr/bin/find $my_path -type f -name $potential_file_names | head -n1`; attempt 2:      $first_file = `/usr/bin/find $my_path -type f -name $potential_file_names '|' head -n1`; attempt 3:     $first_file = `/usr/bin/find $my_path -type f -name $potential_file_names \\| head -n1`; 

the $potential_file_names variable string wildcard characters return file in directory that's in format "filexxx.txt" 'xxx' 3 digit number.

$potential_file_names = 'file???.txt'; 

the first attempt doesn't work because perl appears take exception pipe returns error, "sh[2]: 0403-057.

first attempt output:

file001.txt file002.txt file003.txt file004.txt file005.txt 

the second , third attempts fail. error them is, "sh[2]: |: not found."

the output second , third attempts same first attempt.

is possible use find command piped head return first file in directory i'm searching (in case, "file001.txt"?

update should mention file names may not start 001, i'll need oldest file. files created sequentially, grabbing first file using find , piping head -n1 works command line outside script. needs oldest/first file because i'll deleting files using loop later in script , needs find oldest/first file each iteration.

thanks.

try this:

    open exe, qq{/usr/bin/find $my_path -type f -name $potential_file_names | head -n1}                     or die qq{error running command $!};     $file = <exe>;     close(exe); 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -