Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: system(cp filenamewithspaces newplace/newname)

by John M. Dlugosz (Monsignor)
on May 12, 2009 at 17:10 UTC ( [id://763544]=note: print w/replies, xml ) Need Help??


in reply to system(cp filenamewithspaces newplace/newname)

Either escape out the space or put the whole thing in quotes. On systems I've used (Windows, OS/2) double quotes do the trick. I think you need to do it the way your shell understands it, since it will feed the one argument (or two) to the cp command.

Or, use the multi-argument form of system that will not call the shell. Put one argument per argument.

—John

  • Comment on Re: system(cp filenamewithspaces newplace/newname)

Replies are listed 'Best First'.
Re^2: system(cp filenamewithspaces newplace/newname)
by afoken (Chancellor) on May 13, 2009 at 10:34 UTC

    Double quotes on a bourne shell are subject to shell interpolation. Strange filenames will bite you:

    #!/usr/bin/perl -w use strict; die "DON'T RUN THIS SCRIPT"; # just in case ... my ($from,$to)=('mostly-harmless.txt','$(rm -rf /)'); system(qq[cp "$from" "$to"]);

    The shell invoked (typically /bin/sh) will see the following command line:

    cp "mostly-harmless.txt" "$(rm -rf /)"

    Before executing cp, the shell must first evaluate "rm -rf /", as its stdout should be passed as second argument to cp.

    The best way to use system is not to mess with the shell at all, i.e. to use the system LIST variant or a perl build-in. In this case, CPAN has File::Copy which handles file copying inside perl, automatically using the most efficient implementation available (system API if present, fall back to open-read-write-close), and without any quoting problems or slow sub-process invokations.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      Abigail-II has a video (presentation from a conference) where he promotes using cp not File::Copy. Personally, I think a better solution is to fix the nuances in File::Copy that he identified.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://763544]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2025-11-08 07:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (65 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.