CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
missing argument?

 



AlexPerl
Novice

Apr 15, 2014, 10:37 AM

Post #1 of 5 (2486 views)
missing argument? Can't Post

Hi, I have this single Linux command in Perl (I don't know Perl well enough to write it):


Code
my $test_tmp = "/tmp"; 
my $dir = "/directory ";
`find $test_tmp/$dir -type f -name "*gds" -exec rm -f {} \;`;


What I'm trying to do is find and remove a file name *gds in the /tmp/directory. I'm invoke this script at the home directory, that's why I give it the $test_tmp/$directory variable. It did not remove and I'm getting the missing argument below. Can anyone help please? Thanks!

find: missing argument to `-exec'


FishMonger
Veteran / Moderator

Apr 15, 2014, 12:16 PM

Post #2 of 5 (2483 views)
Re: [AlexPerl] missing argument? [In reply to] Can't Post

I believe you need to us quotes around the braces.


Code
find $test_tmp/$dir -type f -name "*gds" -exec rm -f '{}' \;


Do you need to delete the files in the sub-directories, or just that one directory?


AlexPerl
Novice

Apr 15, 2014, 12:32 PM

Post #3 of 5 (2481 views)
Re: [FishMonger] missing argument? [In reply to] Can't Post

Thanks, but putting single or double quotes '{}' still have the same problem :(

find: missing argument to `-exec'


FishMonger
Veteran / Moderator

Apr 15, 2014, 1:26 PM

Post #4 of 5 (2475 views)
Re: [AlexPerl] missing argument? [In reply to] Can't Post

There's no need to use shell commands.

Perl has the unlink function for deleting files.
http://perldoc.perl.org/functions/unlink.html

If you don't need to descend a directory tree, then use the glob function to retrieve the list of files.

If you do need to descend a directory tree looking for the files, then use either the File::Find module or the File::Find::Rule module.

File::Find - Traverse a directory tree. http://search.cpan.org/~rjbs/perl-5.18.2/lib/File/Find.pm
File::Find::Rule - Alternative interface to File::Find http://search.cpan.org/~rclamp/File-Find-Rule-0.33/lib/File/Find/Rule.pm


Laurent_R
Veteran / Moderator

Apr 15, 2014, 2:34 PM

Post #5 of 5 (2471 views)
Re: [FishMonger] missing argument? [In reply to] Can't Post

I definitely agree that there is no need to use a shell command, and, BTW, this is not a Perl question, but a shell question.

Granted, the find shell command is very powerful and appealing, but its detailed implementation is not the same from one system to another (or from one shell to another). Therefore, if you want your script to be portable, rather use the Perl built-in functions. They are much more likely to work in all cases.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives