|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. When using mv command the new file's timestamp is not updated.
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
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. 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).
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 –