mv
is a cp
, ln
and mv
are implemented as a single program with hard-linked binaries.
History
Multics provided a file move/rename command named . A version with the contracted name appeared in Version 1 Unix and became part of the X/Open Portability Guide issue 2 of 1987. The version in GNU Core Utilities was written by Mike Parker, David MacKenzie, and Jim Meyering. The command is available in Windows via UnxUtils. The command was ported to IBM i.Options
Most implementations support: *-i
interactive; the command prompts the user to confirm moving each file that would overwrite an existing file; overrides a preceding option
* -f
force overwriting existing files; overrides a preceding option
These options are a part of X/Open Portability Guidelines, later the basis of POSIX and SUS. A POSIX-compliant implementation must support these. Single Unix Specification#1980s: Motivation
Name clashing
When a file is moved to a path that specifies an existing file, the existing file is clobbered by default. If the existing file is not writable but is in a directory that is writable, the command prompts the user to confirm overwrite (if run from a terminal), unless the option is included. Accidental overwriting can be prevented using the GNU implementation-n
(long format: --no-clobber
) option. Alternatively, -u
(--update
) only overwrites destination files that are older than source files, -i
(--interactive
) asks for confirmation upon each name conflict, and -b
(--backup
) renames target files to prevent overwrite.
Ambiguity arises when a file is moved to a path that specifies an existing directory. By default, mv
handles this by moving the file inside the directory. The GNU implementation has a switch for that tries 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 more efficiently than copying the file and then removing the original. On platforms that do not support the rename()
system call, a new Examples
The following renames file or directory foo to bar. This assumes that bar is not an existing directory beforehand. $ mv foo bar The following moves the file or directory foo into the existing subdirectory subdir so that the result is at path subdir/foo. $ mv foo subdir The following moves the file or directory foo into directory subdir with name bar so that the resulting is path subdir/bar. This assumes that subdir/bar is not an existing directory beforehand. $ mv foo subdir/bar The following moves two files/directories, foo and bar, to existing directory subdir. $ mv foo bar subdir Copy be.03 to the bes directory of the mounted volume bkup, then be.03 is removed. In this example, /mnt refers to the directory (the "mount point") over which a file system is mounted. $ mv be.03 /mnt/bkup/bes Same as above, except each file moved out of be.03 is removed individually instead of all being removed at once after the entire copying is finished. $ mv be.03/* /mnt/bkup/bes The following takes longer than expected if /var is on a different file system, as it frequently is, since files will be copied and removed. The shell expands~
to the user's home directory and treats *
as a See also
* * * * * *References
External links
* * {{Core Utilities commands Unix SUS2008 utilities Inferno (operating system) commands IBM i Qshell commands