11.4 mv: Move (rename) files

mv moves or renames files (or directories). Synopses:

mv [option]… [-T] source dest
mv [option]… sourcedirectory
mv [option]… -t directory source

To move a file, mv ordinarily simply renames it. However, if renaming does not work because the destination’s file system differs, mv falls back on copying as if by cp -a, then (assuming the copy succeeded) it removes the original. If the copy fails, then mv removes any partially created copy in the destination. If you were to copy three directories from one file system to another and the copy of the first directory succeeded, but the second didn’t, the first would be left on the destination file system and the second and third would be left on the original file system.

mv always tries to copy extended attributes (xattr), which may include SELinux context, ACLs or Capabilities. Upon failure all but ‘Operation not supported’ warnings are output.

If a destination file exists but is normally unwritable, standard input is a terminal, and the -f or --force option is not given, mv prompts the user for whether to replace the file. (You might own the file, or have write permission on its directory.) If the response is not affirmative, the file is skipped.

Avoid specifying a source name with a trailing slash, when it might be a symlink to a directory. Otherwise, mv may do something very surprising, since its behavior depends on the underlying rename system call. On a system with a modern Linux-based kernel, it fails with errno=ENOTDIR. However, on other systems (at least FreeBSD 6.1 and Solaris 10) it silently renames not the symlink but rather the directory referenced by the symlink. See Trailing slashes.

The mv command replaces destination directories only if they are empty. Conflicting populated directories are skipped with a diagnostic.

The program accepts the following options. Also see Common options.

-b
--backup[=method]

See Backup options. Make a backup of each file that would otherwise be overwritten or removed.

--debug

Print extra information to stdout, explaining how files are copied. This option implies the --verbose option.

-f
--force

Do not prompt the user before removing a destination file. If you specify more than one of the -i, -f, -n options, only the final one takes effect.

-i
--interactive

Prompt whether to overwrite each existing destination file, regardless of its permissions, and fail if the response is not affirmative. If you specify more than one of the -i, -f, -n options, only the final one takes effect.

-n
--no-clobber

Do not overwrite an existing file; silently fail instead. If you specify more than one of the -i, -f, -n options, only the final one takes effect. This option is mutually exclusive with -b or --backup option. See also the --update=none option which will skip existing files but not fail.

--no-copy

If a file cannot be renamed because the destination file system differs, fail with a diagnostic instead of copying and then removing the file.

--exchange

Exchange source and destination instead of renaming source to destination. Both files must exist; they need not be the same type. This exchanges all data and metadata.

This option can be used to replace one directory with another. When used this way, it should be combined with --no-target-directory (-T) to avoid confusion about the destination location. For example, you might use ‘mv -T --exchange d1 d2’ to exchange two directories d1 and d2.

Exchanges are atomic if the source and destination are both in a single file system that supports atomic exchange. Non-atomic exchanges are not yet supported.

If the source and destination might not be on the same file system, using --no-copy will prevent future versions of mv from implementing the exchange by copying.

-u
--update

Do not move a non-directory that has an existing destination with the same or newer modification timestamp; instead, silently skip the file without failing. If the move is across file system boundaries, the comparison is to the source timestamp truncated to the resolutions of the destination file system and of the system calls used to update timestamps; this avoids duplicate work if several ‘mv -u’ commands are executed with the same source and destination. This option is ignored if the -n or --no-clobber option is also specified.

which gives more control over which existing files in the destination are replaced, and its value can be one of the following:

all

This is the default operation when an --update option is not specified, and results in all existing files in the destination being replaced.

none

This is like the deprecated --no-clobber option, where no files in the destination are replaced, and also skipping a file does not induce a failure.

none-fail

This is similar to ‘none’, in that no files in the destination are replaced, but any skipped files are diagnosed and induce a failure.

older

This is the default operation when --update is specified, and results in files being replaced if they’re older than the corresponding source file.

-v
--verbose

Print the name of each file before moving it.

Follow existing symlinks to directories when copying. Use this option only when the destination directory’s contents are trusted, as an attacker can place symlinks in the destination to cause cp write to arbitrary target directories.

--strip-trailing-slashes

Remove any trailing slashes from each source argument. See Trailing slashes.

-S suffix
--suffix=suffix

Append suffix to each backup file made with -b. See Backup options.

-t directory
--target-directory=directory

Specify the destination directory. See Target directory.

-T
--no-target-directory

Do not treat the last operand specially when it is a directory or a symbolic link to a directory. See Target directory.

-Z
--context

This option functions similarly to the restorecon command, by adjusting the SELinux security context according to the system default type for destination files and each created directory.

An exit status of zero indicates success, and a nonzero value indicates failure.