How can I reliably rename a file?

If your operating system supports a proper mv(1) utility or its functional equivalent, this works:

    rename($old, $new) or system("mv", $old, $new);
It may be more portable 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 semantically as a rename(), which preserves meta-information like permissions, timestamps, inode info, etc.

Newer versions of File::Copy export a move() function.


Back to perlfaq5