The mv command is used to rename and move files and directories from one location to another. The syntax for the mv command is as follows:
Syntax:
$ mv [OPTIONS] SOURCE DESTINATION
To move the file file1 from the current working directory to the /tmp directory we would run:
Example:
$ mv file1 /tmp
To rename a file/directory we need to specify the destination file/directory name:
Example:
$ mv file1 file2
$ mv dir1 dir2
In both the above examples, if file2 or dir2 exists, the file1 will be moved inside file2 and dir1 will be moved inside dir2. But if file2 or dir2 does not exist, file1 will be renamed to file2 and dir1 will be renamed to dir2.
NOTE: Very similar to the cp command, mv also uses the option –I for confirmation.