Mv


mv is a Unix command that moves one or more files or directories from one place to another. If both filenames are on the same filesystem, this results in a simple file rename; otherwise the file content is copied to the new location and the old file is removed. Using mv requires the user to have write permission for the directories the file will move between. This is because mv changes the content of both directories involved in the move. When using the mv command on files located on the same filesystem, the file's timestamp is not updated.
On UNIX implementations derived from AT&T UNIX, cp, ln and mv are implemented as a single program with hard-linked binaries. The behavior is selected from the path name. This is a common technique by which closely related commands that have been packaged as a unit allow the user to specify the particular course of the intended action. GNU coreutils does the same.

History

The command first appeared in Version 1 Unix. It also dates back to X/Open Portability Guide issue 2 of 1987.
The version of mv bundled in GNU coreutils was written by Mike Parker, David MacKenzie, and Jim Meyering.

Conflicting existing file

When a filename is moved to an existing filename, the existing file is deleted. If the existing file is not writable but is in a directory that is writable, the mv command asks for confirmation before proceeding, unless the -f option is used.
A related ambiguity arises when a filename is moved to an existing directory. By default, mv would handle this as one trying to move a name inside this directory. GNU mv has a switch for disabling this assumption and try to overwrite the directory instead. An inverse makes the move-to-directory operation explicit.

Moving versus copying and removing

Moving files within the same file system is generally implemented differently than copying the file and then removing the original. On platforms that do not support the rename syscall, a new link is added to the new directory and the original one is deleted. The data of the file is not accessed. All POSIX-conformant systems implement the rename call.
An actual move is dramatically faster than the circuitous copy-and-move procedure. The file's i-number does not change. No permission is required to read the file being moved insofar as—conceptually speaking—it is only cataloguing information that is being changed as a result of the "move." Since the source and target directories are being modified, to wit, entries are being created within the target directory and erased from within the source directory, "write" permission in both directories is required to complete the move. Moving files from one file system to another may fail entirely or may be automatically performed as an atomic copy-and-delete action; the actual details are dependent upon the implementation.
Write permission on the directory being migrated is additionally required in the case where a directory is being re-parented. This is because the ".." entry—which can always be used as an "alias" for its parent if the parent's name is unknown—must be changed within the migrant per se.
"Renaming" a directory from within one parent to within a different parent requires write permission in the directory being named, aside from permission to modify the old and new parents. This is because the i-number for the directory entry ".." changes as a result of the rename.

Options

Most versions of mv support:
These options are a part of X/Open Portability Guidelines, later the basis of POSIX and SUS. All POSIX-compliant mv implementations must support these.

Examples


mv myfile mynewfilename # renames 'myfile' to 'mynewfilename'.
mv myfile ~/myfile # moves 'myfile' from the current directory to user's home directory.
# the notation '~' refers to the user's "home" directory
mv myfile subdir/myfile # moves 'myfile' to 'subdir/myfile' relative to the current directory.
mv myfile subdir # same as the previous command, filename is implied to be the same.
mv myfile subdir/myfile2 # moves 'myfile' to 'subdir' named 'myfile2'.
mv be.03 /mnt/bkup/bes # copies 'be.03' to the mounted volume 'bkup' the 'bes' directory,
# then 'be.03' is removed.
mv afile another /home/yourdir/yourfile mydir
# moves multiple files to directory 'mydir'.
mv /var/log/*z ~/logs # takes longer than expected if '/var' is on a different file system,
# as it frequently is, since files will be copied & deleted
# be careful when using globbable file name patterns containing
# the characters ?* to ensure that the arguments passed to 'mv'
# include [a list
of non-directories and a terminating directory
man mv # displays the complete UNIX manual page for the 'mv' command.

Note that, in the above example, /mnt referred to the directory over which a given file system is mounted. Naming such directories /mnt is a popular convention but is by no means necessary. A "file system" can be thought of as an independent tree that is logically regarded as a unit; its root is "mounted" atop a directory of the administrator's choice. Any previous contents of that directory are invisible, but they are "restored" when the new volume is unmounted.