Perl: How to move/rename file

If you want to move file from one directory/folder to another directory/folder, you can use the rename function of Perl. This function takes OldFileName and NewFileName as parameter, then move the file into NewFileName.

rename($OldFileName,$NewFileName);

If You want to rename the file "deadman.txt" into new name "aliveman.txt", then below line will rename the file.

rename("deadman.txt","aliveman.txt");

If you want to move the file into another directory/folder, then code will be

rename("deadman.txt","c:\\deadman.txt"); # windows
rename("deadman.txt","/root/deadman.txt"); # linux


Although you can use the mv command from Linux by using system() function. Lots of choice and ways to do in Perl.

Comments

Anonymous said…
Thanks, was helpful for Windows.

Gotta watch the double backspaces due to escpapeing.