perl rename failed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl rename failed
# 1  
Old 05-25-2006
perl rename failed

i have a file, which i want to rename, from my current directory to a backupdirectory. My perl code is below
Code:
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?
# 2  
Old 05-25-2006
Please, you have a previous thread on this subject started here:

https://www.unix.com/shell-programming-and-scripting/27580-rename-does-not-work-perl.html

Do not start another one.

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

Code:
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)?
# 3  
Old 05-25-2006
Hi cbkihong,
the trap message is "Invalid cross-device link"
# 4  
Old 05-26-2006
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:

Quote:
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.
# 5  
Old 05-26-2006
Hi cbkihong,

i tried to use the following, and it was successful. Thank you.
Code:
mv($s_filename,$newfile);

As advised by you, i should include a mechanism to capture any trap, how can i do it? Is it
Code:
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.
# 6  
Old 05-26-2006
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

I did not have much experience with this module.
# 7  
Old 05-26-2006
Quote:
Originally Posted by new2ss
As advised by you, i should include a mechanism to capture any trap, how can i do it? Is it
Code:
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

for the manual. For File::Copy, you may also read that manpage for expected return value and error status reporting. It should be similar.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

PERL: In a perl-scripttTrying to execute another perl-script that SETS SOME VARIABLES !

I have reviewed many examples on-line about running another process (either PERL or shell command or a program), but do not find any usefull for my needs way. (Reviewed and not useful the system(), 'back ticks', exec() and open()) I would like to run another PERL-script from first one, not... (1 Reply)
Discussion started by: alex_5161
1 Replies

2. AIX

Perl error : perl: warning: Setting locale failed.

This's my problem perl: warning: Setting locale failed. perl: warning: Please check that your locale settings: LC_ALL = "en_US.UTF-8", LC__FASTMSG = "true", LC_MESSAGES = "", LC_CTYPE = "en_US.UTF-8", LC_TYPE = "en_US.UTF-8", LANG = "EN_US"... (1 Reply)
Discussion started by: bobochacha29
1 Replies

3. HP-UX

[Solved] Unable to rename file in ftp server .Net:FTP perl

Hello All, I am trying to connect to ftp server and get the files. Also i need to rename the file in other ftp dir. rename method is not allowing me to rename the file in other dir. When i tried copy command by using net::FTP:FILE then perl says it is not installed. Can some body help me to... (2 Replies)
Discussion started by: krsnadasa
2 Replies

4. Solaris

Cygwin X Server error: xdmcp fatal error session failed session 23 failed for display

Hi, i got the following error when i tried to access the cygwin x server from a windows XP PC. "xdmcp fatal error session failed session 23 failed for display" Alternatively, when i tried to access the same Cygwin X Server from another windows XP PC which is on a different LAN... (3 Replies)
Discussion started by: HarishKumarM
3 Replies

5. Shell Programming and Scripting

Perl script to rename file,error

#!/usr/bin/perl $FL="ch.txt"; $CFL="mytext.txt"; print "This script will rename textfiles."; mv $FL $CFL; print "Done."; perl file.pl Error: Can't locate object method "mv" via package "ch.txt" (perhaps you forgot to load "ch.txt"?) at file.pl line 7. (5 Replies)
Discussion started by: cola
5 Replies

6. Shell Programming and Scripting

Using Perl to Rename File

Hi, Can someone help me with a perl file to rename some files please? I can do it with regular command line using the below code, but I need to include this in another script and the other script is perl. I know nothing of perl. for file in C* do newfilename=`echo $file | cut -c8-21-`... (8 Replies)
Discussion started by: bbbngowc
8 Replies

7. UNIX for Dummies Questions & Answers

Ok. I've have looked and failed.

What I want seems to me should be simple but so am I so.... I have a Unix server that is running a program we will say logged in as UserA. The program is outputting a bunch of stuff to the terminal. I would like to log in from my machine to the server as Either UserA, UserB or if needed... (2 Replies)
Discussion started by: Dishevel
2 Replies

8. AIX

SFTP Failed---Request for subsystem 'sftp' failed on channel 0

Hi, While I am trying SFTP my machine to another unix machine , it was working fine till 10 min back. But now i am getting the below error "Request for subsystem 'sftp' failed on channel 0" Could you please someone help me to solve or analyise the root cause... Cheers:b:, Mahiban (0 Replies)
Discussion started by: mahiban
0 Replies

9. Solaris

Failed to power up

Dear expert, I have one server,in /var/adm/messages everyday present these messages: Jan 3 06:01:30 SERVERGINTING genunix: WARNING: Device /pci@1d,700000/pci@1/scsi@2,1/sd@5,0 failed to power up. Jan 3 06:01:31 SERVERGINTING scsi: WARNING: /pci@1d,700000/pci@1/scsi@2,1/sd@5,0 (sd45):... (2 Replies)
Discussion started by: fredginting
2 Replies

10. Shell Programming and Scripting

rename does not work in perl

#!/usr/bin/perl -w use POSIX qw(strftime); $PID = strftime "%Y%m%d%H%M%S", localtime; $filename = "original.list"; rename($filename,$filename.$PID)|| print "rename failed\n"; print $filename;The above code has no syntax error. But the renaming does not work. Anybody got a clue? (1 Reply)
Discussion started by: new2ss
1 Replies
Login or Register to Ask a Question