Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

system "copy" call (under linux)

Status
Not open for further replies.

Ba11ousai

Programmer
Oct 15, 2001
1
US
Im trying to copy a file to a new directory, the file name is in a variable ($file). What am i doing worng??:
I've tried these:
system "cp $file ./scratch/file.out";
system "cp `$file` ./scratch/file.out";
help please...
 
Have you tried:
Code:
`cp $file ./scratch/file.out`;

The first system command should have worked I think.. but maybe backticks will have more success.

Also as you are using a relative path are you sure you have permissions to write to scratch/file.out and that a scratch/file.out exists below the directory where your script is located?

Maybe better to use the absolute path? Something like (depending on your config):
Code:
system "cp $file /tmp/scratch/file.out";

Hope that helps
Loon
 
The Perl Cookbook Recipe 9.3 "Copying or Moving a File" suggests using the File::Copy module - you can get the perldoc documentation by doing "perldoc File::Copy". *Or*, the other option it suggests is using the "system" command as you've tried to do - here's how it suggests using system:

system("cp $oldfile $newfile"); ### unix

The Programming Perl 2nd edition p.230 describes using the "system" command - here's what it suggests:

@args = ("command", "arg1", "arg2");
system(@args) == 0
or die "system @args failed: $?";

So, following that, I would do something like this for your cp command:

@args = ("cp", "$oldfile", "$newfile");
system(@args) == 0
or die "system @args failed: $?";

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
Remember that, if you're executing the command from a cgi script, your current directory is your cgi directory. So unless the file you're trying to copy is in your cgi directory you'll need to include a path to that too. It's usually safer to use absolute paths in a cgi script. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
I second Tracy's comment - whenever possible I *always* use absolute paths with filenames, whether I'm writing cgi scripts or just plain perl scripts, and I rarely(never say never) have problems.
Hardy Merrill
Mission Critical Linux, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top