Perl file moving: How to move a file using Perl

Perl move file FAQ: How do I move a file in Perl?

Surprisingly, you can move a file in Perl with the File::Copy module. Yes, the Perl file copy module also handles file moving.

A Perl file move example

Here’s a quick example:

# a "perl file move" example
use File::Copy;

# somewhere in the code ...
$original_file = 'original-filename.txt';
$new_file = 'new-filename.txt';

# the perl move file function
move($original_file, $new_file);

Perl move file: adding the “die” operator

You can also use the die operator during the Perl file moving process, like this:

use File::Copy;

$original_file = 'original-filename.txt';
$new_file = 'new-filename.txt';

# the perl file move function
move($original_file, $new_file) or die "The move operation failed: $!";

More information

For more Perl file-moving information, here’s another link to the File::Copy documentation.