icon Top 9 categories map      RocketAware > Perl >

How can I reliably rename a file?

Tips: Browse or Search all pages for efficient awareness of Perl functions, operators, and FAQs.


Search Perl pages


By activity
Professions, Sciences, Humanities, Business, ...

User Interface
Text-based, GUI, Audio, Video, Keyboards, Mouse, Images,...

Text Strings
Conversions, tests, processing, manipulation,...

Integer, Floating point, Matrix, Statistics, Boolean, ...

Algorithms, Memory, Process control, Debugging, ...

Stored Data
Data storage, Integrity, Encryption, Compression, ...

Networks, protocols, Interprocess, Remote, Client Server, ...

Hard World
Timing, Calendar and Clock, Audio, Video, Printer, Controls...

File System
Management, Filtering, File & Directory access, Viewers, ...


How can I reliably rename a file?

Well, usually you just use Perl's rename() function. But that may not work everywhere, in particular, renaming files across file systems. If your operating system supports a mv(1) program or its moral equivalent, this works:

    rename($old, $new) or system("mv", $old, $new);

It may be more compelling to use the File::Copy module instead. You just copy to the new file to the new name (checking return values), then delete the old one. This isn't really the same semantics as a real rename(), though, which preserves metainformation like permissions, timestamps, inode info, etc.

Source: Perl FAQ: Files and Formats
Copyright: Copyright (c) 1997 Tom Christiansen and Nathan Torkington.
Next: How can I lock a file?

Previous: How can I open a file with a leading ">" or trailing blanks?

(Corrections, notes, and links courtesy of RocketAware.com)

[Overview Topics]

Up to: File Information

Rapid-Links: Search | About | Comments | Submit Path: RocketAware > Perl > perlfaq5/How_can_I_reliably_rename_a_file.htm
RocketAware.com is a service of Mib Software
Copyright 2000, Forrest J. Cavalier III. All Rights Reserved.
We welcome submissions and comments