Rename files and folders

mv is a command-line program for moving and renaming files and folders.

In this tutorial we will be using mv to rename files and folders. There is a companion tutorial called [[Move Files and Folders]].

Note: The terms "folder" and "directory" mean the same thing--they can be used interchangeably.

Syntax

mv [source] [target]
  • [source] -- Filepath of the file or folder you want to rename.
  • [target] -- The new name for your [source] file/directory.

Example #1 -- Rename file

mv /root/company.png logo.png 

New Filepath: /var/www/logo.png

Example #2 -- Rename Folder

mv /root/foo/ example

New Filepath: /var/www/example/

Example #3 -- Rename file and move to new location

mv instructions.txt /var/www/html/readme.txt

New Filepath: /var/www/html/readme.txt

Example #4 -- Rename file and move to new location

mv instructions.txt /var/www/html/readme.txt

New Filepath: /var/www/html/readme.txt

Example #4 -- Rename folder and move to new location

In this example, "/var/www/html/" is a directory that already exists. And because that directory exists, the directory name that follows will be the new name for the "html" directory.

mv /root/foo/ /var/www/html/example/

New Filepath: /var/www/html/example/

Caution

You can not create a new folder using the mv program.

For example, if want to move an empty folder "/root/test" into "/var/www/html/", you can not create a new directory by doing something like this...

mv /root/test/ /var/www/html/books/test/

..or else you will get an error message: "No such file or directory"--meaning the "books" directory does not exist.