Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

13. Directory Operations > Renaming Files

Renaming Files

Giving an existing file a new name is simple with the rename function:

rename 'old', 'new';

This is similar to the Unix mv command, taking a file named old and giving it the name new in the same directory. You can even move things around:

rename 'over_there/some/place/some_file', 'some_file';

Some people like to use the fat arrow that you saw in Chapter 6 (The Big Arrow) so they remind themselves which way the rename happens:

rename 'over_there/some/place/some_file' => 'some_file';

This moves a file called some_file from another directory into the current directory, provided the user running the program has the appropriate permissions.[324] Like most functions that request something of the operating system, rename returns false if it fails, and sets $! with the operating system error, so you can (and often should) use or die (or or warn) to report this to the user.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint