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

How can I get a script to act just like the Unix mv where it does moving or renaming the files given as command line arguments?? Here is some sort of attempt at it. Please help me get this right.
sub mov { !system "/bin/mv", @_; }
Then I call the subroutine mov somewhere but I just cant put this together the right way.

Replies are listed 'Best First'.
Re: Move or rename script
by Molt (Chaplain) on May 24, 2002 at 14:00 UTC

    Okay, that should work exactly like /bin/mv, but that's because it is /bin/mv with a wrapper.

    If you're looking into doing the move in Perl look at File::copy, which has a rather nice move method which does exactly what you seem to want to do, at least in skeleton- if you need the same command line options you can add them.

    If you're after exactly the same functionality then look at Perl Power Tools, where people have tried to rebuild an mv in Perl with full functionality.

    Update: Wrote File::find rather than File::copy in first version. Oops.

      Actually I would like to try and do it within a perl script without using modules similiar to how I did my create directory:
      #!/usr/local/bin/perl sub mak { !system "/bin/mkdir", @_; } print "Enter name of Directory you want to create: "; chomp($newb = <STDIN>); mak $newb; print "$newb directory created.\n";
      Can the same be done with the move command except I want to use command line arguments when the script is invoked???

        You say you want to "move or rename" something.

        Let's see

         # perldoc -f move
         No documentation for perl function `move' found
        
        
        ok, so let's try the other
        
         # perldoc -f rename
            rename OLDNAME,NEWNAME
                    Changes the name of a file; an existing file NEWNAME will be
                    clobbered. Returns true for success, false otherwise.
                    ...
        

          Jenda

        use backticks `` to make your system calls. (sorry I cant find a good link)

        any arguments can just be passed into the backtick.

        i.e. a recursive list of files that could be split with "\n" into an array.<br /
        @foo = `find $path -name $ftype`;

        And for move:

        $where = '/home/iordy/public_html/';
        $what = '/home/iordy/whatever.pl';
        @foo = `mv $args $what $where`;
(wil) Re: Move or rename script
by wil (Priest) on May 24, 2002 at 14:02 UTC
    Have you looked into the module Meta::Utils::File::Move? This should do everything you're asking for. It's a preety basic module in terms of usage but it does the job well. The module uses a few functions from File::Copy so you'll probably need to pick that up too.

    Hope this helps.

    - wil