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.

Warning: 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.

Note: mv will only replace empty directories in the destination. 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.

-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 similar to the --no-clobber option, in that no files in the destination are replaced, but also skipping a file does not 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.

--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.