http://www.perlmonks.org?node_id=812079

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

I don't use perl much. I tried to use it to rename some files and I can't get the rename function to work. The problem seems to be file permissions, but even if I use sudo, or chmod the permissions to 755, it still won't work. Does this have something to do with OSX and Darwin? I am running 10.6.2 on an Intel Macbook.

My code (the directory "kennel" contains two files: cat1, cat2):

opendir(DIR,"kennel"); while ($file = readdir(DIR)) { next unless $file =~ /cat/; my $newfile = $file; $newfile =~ s/cat/dog/; rename($file, $newfile) || print "Don't have permission to rename. +\n"; } closedir(DIR);
When run, this code prints the error message (i.e. does not rename files)

Replies are listed 'Best First'.
Re: rename doesn't work on OSX?
by toolic (Bishop) on Dec 10, 2009 at 02:09 UTC
    You can try to get a more specific error message by also printing the $! variable:
    rename($file, $newfile) || print "Don't have permission to rename: $!\ +n";

    According to the documentation for rename,

    Behavior of this function varies wildly depending on your system implementation. For example, it will usually not work across file system boundaries, even though the system mv command sometimes compensates for this. Other restrictions include whether it works on directories, open files, or pre-existing files. Check perlport and either the rename(2) manpage or equivalent system documentation for details. For a platform independent move function look at the File::Copy module.
    You could try File::Copy.
Re: rename doesn't work on OSX?
by GrandFather (Saint) on Dec 10, 2009 at 02:10 UTC

    permissions is a good guess, but

    rename($file, $newfile) || print "Rename $file to $newfile failed: + $!.\n";

    will give you a lot more information including what the system thinks the problem is.


    True laziness is hard work
Re: rename doesn't work on OSX?
by ikegami (Patriarch) on Dec 10, 2009 at 03:07 UTC
    The actual error (as indicated by $!) is that the file you asked to rename doesn't exist. You're trying to rename kennel/cat, but you're telling Perl and the system to rename cat.
Re: rename doesn't work on OSX?
by vitoco (Hermit) on Dec 10, 2009 at 11:59 UTC

    File path is missing from parameters. Try:

    my $dir = "kennel"; opendir(DIR,$dir) or die "Cannot read $dir\n"; while ($file = readdir(DIR)) { next unless $file =~ /cat/; my $newfile = $file; $newfile =~ s/cat/dog/; rename("$dir/$file", "$dir/$newfile") || print "Don't have permiss +ion to rename.\n"; } closedir(DIR);

      @vitoco and @ikegami -- the file path! Right! It's usually the simplest little things that trip me up!

      Thank you!!

Re: rename doesn't work on OSX?
by desemondo (Hermit) on Dec 10, 2009 at 03:01 UTC
    If all else fails you should be able to use your OS's rename function via an exec , system , or `rename $file1 $file2` (backtick) workaround... I'd use that only as a last resort though...