xmanamit has asked for the wisdom of the Perl Monks concerning the following question:

Hi ,

I was trying to mv a file in unix in the following way:

system("mv $file_name $different_path/$file_name");
But this doesn't work, if I try it in the same dir i.e. renaming the file it works. I could move the file using File::copy module but couldn't understand why this doesn't work. Any comments about this ??

Thanks, Amit

Replies are listed 'Best First'.
Re: unix mv command
by clinton (Priest) on Jun 15, 2007 at 11:09 UTC
    As japhy said, the filenames may contain characters that are messing up your command line. For this reason, you should use this form of system :
    system($cmd,@args)
    But instead, why don't you just use File::Copy:

    File::Copy::move( $old, $new ) or die $!;
    That will try to rename the file, if it can (ie if the destination file is on the same partition as the original), otherwise it'll copy it and delete the original, or it'll tell you why.

    Clint

    UPDATE (doh): I meant move not copy

Re: unix mv command
by derby (Abbot) on Jun 15, 2007 at 12:40 UTC

    Are you checking for failure as outlined in the docs for system (perldoc -f system)?

    system("mv $file_name $different_path/$file_name"); if ($? == -1) { print "failed to execute: $!\n"; } elsif ($? & 127) { printf "child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without'; } else { printf "child exited with value %d\n", $? >> 8; }
    -derby
Re: unix mv command
by japhy (Canon) on Jun 15, 2007 at 11:04 UTC
    I have no idea what's in those variables, so I can't really tell you. Perhaps $different_path has spaces in it?

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
      I have tested the paths, and they are fine as I used the perl mv command using File::copy module with the same variables. But can't understand why this doesn't work.
        If you let us see the paths, then we may be able to explain it.

        Clint

        I have tested the paths, and they are fine as I used the perl mv command using File::copy module with the same variables. But can't understand why this doesn't work.

        Non sequitur. Suppose that $from='foo' and $to='bar baz'. If you pass these arguments to F::C's mv(), then it will try to move the the file named foo to that called bar baz - with a space in it. If you do

        system("mv $from $to");

        as in your first post, a shell will be started which in turn will call the external mv program with the three cmd line arguments foo, bar and baz. (Well, upon a closer look to the docs it seems that it will call the shell only if there are shell metacharacters, but the final result won't change.) This may not be your case, but it serves as an instructive example. Alternatively use the LIST form of system, which will avoid the shell in any case and more likely do the Right Thing™ - and check its return value:

        0 == system 'mv', $from, $to or warn "Something wrong: $?\n";
Re: unix mv command
by mantra2006 (Hermit) on Jun 15, 2007 at 12:38 UTC
    Hi
    You have to construct both paths before using system with mv command, the best way as "Clint" suggested using File::Copy::move command.
    untested code my $src_filename = "/home/abc/abc.txt"; my $dst_filename = "/xyz/bcd/abc.txt"; $ret = system("mv $src_filename $dst_filename"); print "Move Status : $ret";
    Thanks & Regards
    Sridhar

    "Coming together is a beginning. Keeping together is progress. Working together is success.”- Henry Ford
Re: unix mv command
by sago (Scribe) on Jun 15, 2007 at 11:52 UTC

    For example if you want to move the file aaa.txt to the
    folder test, use the below one in command prompt.
    C:\perl\bin>perl -MExtUtils::Command -e mv C:/aaa.txt C:/test/aaa.txt
    The move operation will be successfull.