in reply to Re: What is the best way to move a directory?
in thread What is the best way to move a directory?

The rename system call (upon which the Perl rename is built) cannot rename files across different devices (i.e. across mount points, say from /home/foo/bar to /nfsmount/baz). See File::Copy and its move routine.

  • Comment on Re^2: What is the best way to move a directory?

Replies are listed 'Best First'.
Re^3: What is the best way to move a directory?
by wenD (Beadle) on Nov 29, 2006 at 11:46 UTC

    Thanks! The fact that File::Copy is one of the standard (already installed) Perl modules is a plus.

    ---------------------------- use File::Copy; my $source_dir = '/home/my/this_dir'; my $destination_dir = '/usr/local/elsewhere/this_dir'; move("$source_dir", "$destination_dir"); ----------------------------

    And I think this

    ----------------------------
    use File::Copy;
    use Carp qw(croak);
    
    move_reliable("$source_dir", "$destination_dir");
    
    sub move_reliable {
        my ( $source, $destination ) = @_;
        my $source_size = (-s $source) || 0;
    
        move( $source, $destination )
            || croak("move_reliable($source, $destination) failed: $!");
        my $destination_size = (-s $destination) || 0;
        croak(
            "move_reliable($source, $destination) failed copied $destination_size bytes out of $source_size"
            )
            if ( $source_size != $destination_size );
        1;
    }
    ----------------------------
    

    from Leon Brocard's File::Copy::Reliable is also nice.