|The Wikibook Guide to Unix has a page on the topic of: Commands/File System Utilities#mv|
mv (short for move) is a Unix command that moves one or more files or directories from one place to another. Since it can "move" files from one filename to another, it is also used to rename files. Using mv requires the user to have write permission for the directories the file will move between. This is because mv changes the file's location by editing the file list of each directory.
Conflicting existing file
When a filename is moved to an existing filename (in the same directory), 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
move versus copy and remove
Usually moving files within the same file system is not the same as copying and then removing the original. First a new link is added to the new directory then the original link is deleted. The data of file is not accessed. This is much faster than physical copy and remove the file-content. The file still has the same inode.
When moving files to a different file system, all files are copied and then all files are removed. If the copy fails (as in not enough space) none of the original files are removed and all of the copied files remain (and the volume remains full!). If the files are on one volume, an out of space condition cannot occur.
You cannot copy a file if you do not have read permissions, but you can move it if you have write permission to its old and new directories.
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).
Most versions Single Unix Specification#1980s: Motivation of mv support:
- -h help by displaying additional options supported. Use man mv for details for the version on the system you are using.
- -i interactively process, write a prompt to standard error before moving a file that would overwrite an existing file. If the response from the standard input begins with the character`y' or `Y', the move is attempted. (overrides previous -f or -n options.)
- -n no overwriting of existing files. (overrides previous -f or -i options.)
- -f force overwriting the destination (overrides previous -i or -n options).
- -v verbose, shows filenames/directory names after they are moved.
Additional options (Use man mv for details):
- -u update only when the original is newer than the destination or when the destination doesn't exist.
- -b backup of existing destination using default ~ suffix.
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 -v Jun* bkup/06 # displays each filename as it is moved to the subdirectory 'bkup/06'. 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. mv --help # shows a concise help about the syntax of the command. mv !(bkup) bkup # mv all files and dirs to the dir in the same level # move "all the files and directories"(except bkup) to the "bkup" directory, # (assuming , if "all the files and directories" and "bkup" directory are in the same directory ) mv !(bkup1|bkup2) bkup2 # move "all the files and directories"(except bkup1, bkup2) to the "bkup2" directory. man mv # displays complete manual for mv.
- The Single UNIX® Specification, Issue 7 from The Open Group : move files – Commands & Utilities Reference,
- Linux User Commands Manual : move (rename) files –