# perl rename failed

**URL:** <https://community.unix.com/t/perl-rename-failed/161955>\
**Category:** Shell Programming and Scripting\
**Created:** [May 25, 2006, 6:39am UTC](https://community.unix.com/t/perl-rename-failed/161955 "2006-05-25T06:39:12Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![new2ss](https://community.unix.com/letter_avatar/new2ss/32/6_449c533f586939b6e5f246632a46421f.png) [@new2ss](https://community.unix.com/u/new2ss)\
**Post date:** [May 25, 2006, 6:39am UTC](https://community.unix.com/t/perl-rename-failed/161955/1 "2006-05-25T06:39:12Z")

</div>

i have a file, which i want to rename, from my current directory to a backupdirectory. My perl code is below

```nohighlight
rename($s_filename,$backup.$s_filename ) 

```

all my variables are defined. but the rename failed. the file permission my file is "-rw-rw-r--". could this be a problem?

I tried to use the mv command from the shell enviromnent, it works. could anyone advise the cause?

---

<div class="post-metadata">

**Author:** ![cbkihong](https://community.unix.com/letter_avatar/cbkihong/32/6_449c533f586939b6e5f246632a46421f.png) [@cbkihong](https://community.unix.com/u/cbkihong)\
**Post date:** [May 25, 2006, 9:26pm UTC](https://community.unix.com/t/perl-rename-failed/161955/2 "2006-05-25T21:26:36Z")

</div>

Please, you have a previous thread on this subject started here:

> **[rename does not work in perl](https://www.unix.com/shell-programming-and-scripting/27580-rename-does-not-work-perl.html)**
>
> pre { overflow:scroll; margin:2px; padding:15px; border:3px inset; margin-right:10px; } Code: #!/usr/bin/perl -w use POSIX qw(strftime); \\\\$PID = strftime "%Y%m%d%H%M%S", localtime; \\\\$filename | The UNIX and Linux Forums

Do not start another one.

As I said in that post, moving a file involves directory permissions, not file permissions. Try instead

```plaintext
if (!rename($s_filename,$backup.$s_filename )) {
    print $!, "\n";
}

```

Is anything shown (in fact you should always trap failures like this when dealing with file operations)?

---

<div class="post-metadata">

**Author:** ![new2ss](https://community.unix.com/letter_avatar/new2ss/32/6_449c533f586939b6e5f246632a46421f.png) [@new2ss](https://community.unix.com/u/new2ss)\
**Post date:** [May 25, 2006, 10:01pm UTC](https://community.unix.com/t/perl-rename-failed/161955/3 "2006-05-25T22:01:30Z")

</div>

Hi cbkihong,  
the trap message is "Invalid cross-device link"

---

<div class="post-metadata">

**Author:** ![cbkihong](https://community.unix.com/letter_avatar/cbkihong/32/6_449c533f586939b6e5f246632a46421f.png) [@cbkihong](https://community.unix.com/u/cbkihong)\
**Post date:** [May 26, 2006, 1:32am UTC](https://community.unix.com/t/perl-rename-failed/161955/4 "2006-05-26T01:32:35Z")

</div>

Sounds like you are trying to move a file between filesystems.

As Perl's rename is tied to the rename(2) system call, please check out the manpage for your system to check if cross-device move is not allowed. If so, then you may need to copy the file to the destination, and then remove the original one.

Don't overlook the footnote at the rename() function from the Perl documentation:

> [@](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![new2ss](https://community.unix.com/letter_avatar/new2ss/32/6_449c533f586939b6e5f246632a46421f.png) [@new2ss](https://community.unix.com/u/new2ss)\
**Post date:** [May 26, 2006, 6:21am UTC](https://community.unix.com/t/perl-rename-failed/161955/5 "2006-05-26T06:21:29Z")

</div>

Hi cbkihong,

i tried to use the following, and it was successful. Thank you.

```nohighlight
mv($s_filename,$newfile);

```

As advised by you, i should include a mechanism to capture any trap, how can i do it? Is it

```nohighlight
if ( mv($s_filename,$newfile) )
{print "error";}

```

i read somewhere that if a system command fails, it sets a high bit, so to test for failure, the above way is suggested.

I realise even if i do not include the mechanism, and it fails, the error message will be printed, so i still need to explicitly include the mechanism?

I believe its more of a program design issue, would appreciate your advise.

---

<div class="post-metadata">

**Author:** ![cbkihong](https://community.unix.com/letter_avatar/cbkihong/32/6_449c533f586939b6e5f246632a46421f.png) [@cbkihong](https://community.unix.com/u/cbkihong)\
**Post date:** [May 26, 2006, 9:23am UTC](https://community.unix.com/t/perl-rename-failed/161955/6 "2006-05-26T09:23:52Z")

</div>

There is officially not a "mv" function in Perl. Do you know where yours come from?

Or did you use this?  
[http://perldoc.perl.org/File/Copy.html](http://perldoc.perl.org/File/Copy.html)

I did not have much experience with this module.

---

<div class="post-metadata">

**Author:** ![cbkihong](https://community.unix.com/letter_avatar/cbkihong/32/6_449c533f586939b6e5f246632a46421f.png) [@cbkihong](https://community.unix.com/u/cbkihong)\
**Post date:** [May 26, 2006, 9:35am UTC](https://community.unix.com/t/perl-rename-failed/161955/7 "2006-05-26T09:35:03Z")

</div>

> [@new2ss](#):
>
> As advised by you, i should include a mechanism to capture any trap, how can i do it? Is it
> 
> ```plaintext
> if ( mv($s_filename,$newfile) )
> {print "error";}
> 
> ```
> 
> i read somewhere that if a system command fails, it sets a high bit, so to test for failure, the above way is suggested.
> 
> I realise even if i do not include the mechanism, and it fails, the error message will be printed, so i still need to explicitly include the mechanism?
> 
> I believe its more of a program design issue, would appreciate your advise.

For system calls, such as rename() as you used, check $! for error number (or string, depending on how you use) as in my earlier post. Please read

[http://perldoc.perl.org/perlvar.html](http://perldoc.perl.org/perlvar.html)

for the manual. For File::Copy, you may also read that manpage for expected return value and error status reporting. It should be similar.

---

<div class="post-metadata">

**Author:** ![new2ss](https://community.unix.com/letter_avatar/new2ss/32/6_449c533f586939b6e5f246632a46421f.png) [@new2ss](https://community.unix.com/u/new2ss)\
**Post date:** [May 28, 2006, 9:42am UTC](https://community.unix.com/t/perl-rename-failed/161955/8 "2006-05-28T09:42:28Z")

</div>

i included this line in the beginnining of my script.

```nohighlight
use SHELL qw(mv);
mv ( $oldfile, $newfile);

```

---

<div class="post-metadata">

**Author:** ![cbkihong](https://community.unix.com/letter_avatar/cbkihong/32/6_449c533f586939b6e5f246632a46421f.png) [@cbkihong](https://community.unix.com/u/cbkihong)\
**Post date:** [May 28, 2006, 10:02am UTC](https://community.unix.com/t/perl-rename-failed/161955/9 "2006-05-28T10:02:30Z")

</div>

I would like to remind you of a warning that has been in the documentation concerning the "Shell" module:

> **[Shell - run shell commands transparently within perl - metacpan.org](https://metacpan.org/pod/Shell)**
>
> run shell commands transparently within perl

> [@](#):
>
> This package is included as a show case, illustrating a few Perl features. It shouldn't be used for production programs. Although it does provide a simple interface for obtaining the standard output of arbitrary commands, there may be better ways of achieving what you need.
> 
> ...
> 
> Since Shell.pm and all of the aforementioned techniques use your system's shell to call some local command, none of them is portable across different systems. Note, however, that there are several built in functions and library packages providing portable implementations of functions operating on files, such as: glob, link and unlink, mkdir and rmdir, rename, File::Compare, File::Copy, File::Find etc.

That's why the File::Copy module came to my mind instead of Shell.

---

<div class="post-metadata">

**Author:** ![new2ss](https://community.unix.com/letter_avatar/new2ss/32/6_449c533f586939b6e5f246632a46421f.png) [@new2ss](https://community.unix.com/u/new2ss)\
**Post date:** [May 28, 2006, 10:59pm UTC](https://community.unix.com/t/perl-rename-failed/161955/10 "2006-05-28T22:59:57Z")

</div>

Hi cbkihong,

thanks for the reminder. I overlooked the portability issue. i think i'll use the below code instead.

```nohighlight
use File::Copy;
move("$old","$new") or print "$!";

```

Appreciated your help. thanks again.
