Darius
Darius

Reputation: 155

Copy/move files with perl

I have a problem with copying/moving files with perl. I want to move a directory with sub-directories into an other directory. Both are given in absolute paths.
What I do is:

system("mv $source $destination")  

where $source and $destination are my source and destination folders.

I also tried it with:

system("cp -r $source $destination")  

and with all possible options, but every time I try it gives me the following output:

sh: line 1: $destination: is a directory

where the $destination is my destination path.

What am I doing wrong?

Upvotes: 0

Views: 2223

Answers (1)

ikegami
ikegami

Reputation: 385789

Instead of executing

cp -r source destination

You are executing

cp -r source
destination

$source appears to contain a trailing newline. Adding chomp($source); is probably the correct fix.


By the way, you aren't building your shell command correctly. You should be using the following:

use String::ShellQuote qw( shell_quote );

my $shell_cmd = shell_quote("cp", "-r", "--", $source, $destination);
system($shell_cmd);

That said, there's no reason to involve a shell at all, so you should be using the following:

system("cp", "-r", "--", $source, $destination)

Upvotes: 4

Related Questions