Single file copy

I would like to copy a single image file (.jpg) from one directory to another directory with a different file name (same extension).

My source perl program is located in: public/cgi-bin/
My source image directory is located: public/source/dir1/
My destination directory is located: public/source/

I have already tried the File::Copy and works great with my active perl debugger, but does not work on the server, so I would like to use a system command in Perl to copy this file.
ianbernsAsked:
Who is Participating?
 
b2piConnect With a Mentor Commented:
system('/bin/cp /public/source/dir1/source.jpg /public/source/dest.img');

- or -

`/bin/cp /public/source/dir1/source.jpg /public/source/dest.img`

- or -

qx { /bin/cp /public/source/dir1/source.jpg /public/source/dest.img};

## If you're on win32, use copy rather than cp

A more interesting question is why File::Copy doesn't work. What error
message do you get?

[Assuming you're already set up for HTML output]

use File::Copy;

copy("/public/source/dir1/source.jpg" "/public/source/dest.img")
    || die "File::Copy failed: $!";

0
 
ianbernsAuthor Commented:
Adjusted points to 40
0
 
ianbernsAuthor Commented:
The File::Copy fails in two ways.

My simple Perl program looks like the following:

#!/usr/bin/perl5

use File::Copy;

$sourcefile = "../source/dir1/image1.jpg";
$destinationfile = "../source/dest.jpg";
copy ($sourcefile, $destinationfile);

exit;

1st error:
use File::Copy;
  (the syntax error is NEXT TWO TOKENS 'use File' )

2nd error
copy ($sourcefile, $destinationfile);
  (the syntax error is NEXT TWO TOKENS 'copy (' )


If using a 'system' command, syntaxwise, how would one write the copy command if both source and destination path/filenames were given in variables?
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
b2piCommented:
what is the output of

/usr/bin/perl5 -v


0
 
ianbernsAuthor Commented:
/usr/bin/perl5 -v

its output when executed:

This is perl, version 5.003_02
        + two suidperl security patches

Copyright 1987-1996, Larry Wall

Wrong perl version maybe for the File::Copy  function?
0
 
b2piCommented:
This isn't making sense (Although I'd recommend upgrading your perl in
any case)

You say if you run it from the command line, you get those parse
errors? Wait a minute... those aren't perl error messages, those are
shell errors...

Are you sure your shebang (#!) line is proper?  What happens if,
assuming your script name is perlcopy.pl, you run

%/usr/bin/perl5 -w perlcopy.pl

instead of

#perlcopy.pl

??
0
 
ianbernsAuthor Commented:
/usr/bin/perl5 -v

its output when executed:

This is perl, version 5.003_02
        + two suidperl security patches

Copyright 1987-1996, Larry Wall

Wrong perl version maybe for the File::Copy  function?
0
 
ozoCommented:
Those look more like perl4 errors.  (assuming you paraphrased the error messages slightly)
Do you get the same error running from the command line?
What do you get if you run

#!/usr/bin/perl5
print $];

on the server?
0
 
ianbernsAuthor Commented:
Found my problem:

1.  File::Copy [ie.  copy($newdir, $destination_dir) ]  does work, it helps if you have 'write' access to the directory you're writing to (a simple chmod fix).

2.  system (test$); where test$ = "/bin/cp $newdir $destination_dir"; also works!
 note: $newdir, and $destination_dir are simple path/filename string variables for flexibility.

Lost some points here!  Anyhow  'b2pi' thanks for the help.....

0
 
ianbernsAuthor Commented:
Thanks b2pi, I'll have more idiot questions
0
 
b2piCommented:
> 1. File::Copy [ie.copy($newdir, $destination_dir) ]does work, it
 >    helps if you have 'write' access to the directory you're writing to
 >    (a simple chmod fix).

Yes, that's true, but it doesn't explain the error messages you were
getting.

 > 2.system (test$); where test$ = "/bin/cp $newdir $destination_dir";
 >   also works!

For even more flexibility, you might try

my($copy) = '/bin/cp';

then, you can do

system("$copy $newdir $destination_dir");


0
 
ozoCommented:
 system($copy,$newdir,$destination_dir);
should be less likely to do nasty things if you have shell metacharacters in $newdir or $destination_dir
but
  use File::Copy;
should be more flexible in working no matter what cp is named on your system
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.