mv

mv (short for move) 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 each related directory. 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 hardlinked binaries. The behavior is selected from the last path component name in argv[0].

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 (if run from a terminal) before proceeding, unless the -f (force) option is used

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 file is not accessed.

This not only is much faster than physically copying the file contents; the file's inode number doesn't change and no read permission for the file is required, though write permission for both source and destination directories is.

Depending on implementation, moving files across different file systems either fails automatically or is implemented by copying files and subsequently removing the originals.

You can rename a directory you do not have write permission to, but you cannot move it to a different parent directory because the directory entry ".." cannot be changed. If you lack write or execute permission to a non-empty directory, you cannot delete this directory (since you cannot delete its contents).

Options

Most versions [1] of mv support:

Examples

 mv myfile mynewfilename     # renames 'myfile' to 'mynewfilename'.
 mv myfile ~/myfile          # moves 'myfile' from the current directory to 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.
 
 man mv                      # displays complete manual for mv.

See also

References

  1. Single Unix Specification#1980s: Motivation

External links

The Wikibook Guide to Unix has a page on the topic of: Commands/File System Utilities#mv