mv

Move (rename) a file.

Format

   mv [options] existing-file new-filename
   mv [options] existing-file-list directory
   mv [options] existing-directory new-directory

Summary

The mv utility moves or renames one or more files. It has three formats. The first renames a single file with a new filename you supply. The second renames one or more files so that they appear in a specified directory. The third renames a directory.

Arguments

In the first form of mv, the existing-file is a pathname that specifies the ordinary file that you want to rename. The new-filename is the new pathname of the file.

In the second form, the existing-file-list contains the pathnames of ordinary files that you want to rename, and the directory specifies the new parent directory for the files. The files you rename will have the same simple filenames as the simple filenames of each of the files in the existing-file-list but new absolute pathnames.

The third form renames the existing-directory with the new-directory name. This form only works when the new-directory does not already exist.

Options

-f force This option causes mv to complete a move regardless of whether or not you have write access permission for a file. See Notes below.
-i interactive This option causes mv to prompt you if a move would overwrite an existing file. If you respond y or yes, the move proceeds; otherwise, the file is not moved.

Notes

The UNIX system implements mv as ln and rm. When you execute the mv utility, it first makes a link (ln) to the new file and then deletes (rm) the existing-file. If the new-file already exists, mv deletes it before creating the link.

As with rm, you must have execute and write access permission to the parent directory of the existing-file, but you do not need read or write access permission to the file itself. If the move will overwrite an existing file that you do not have write permission for, mv displays the access permission and waits for a response. If you enter y or yes, mv renames the file; otherwise it does not. If you use the -f option, mv will not prompt you for a response - it will go ahead and overwrite the file.

If the existing-file and the new-file or directory are on different file systems, the UNIX system implements mv as cp and rm. In this case, mv actually moves the file instead of just renaming it. After a file is moved, the user who moved the file becomes the owner of the file.

The mv utility will not move a file onto itself.

Examples

The first command line renames letter, a file in the working directory, as letter.1201.

z123456@turing:~$ mv letter letter.1201

The next command line renames the file so that it appears, with the same simple filename, in the directory /home/turing/z123456/archives, a subdirectory of the working directory.

z123456@turing:~$ mv letter.1201 archives

The next example renames all the files in the working directory whose names end with .cpp so they appear in the /home/turing/z123456/backup directory.

z123456@turing:~/CS241/Assign1$ mv *.cpp ~/backup