The mv command moves, or renames, files and directories on your filesystem.
Rename a file named source to destination:
mv [options] [-T] source destination
Move source file(s) to a directory named destination:
mv [options] source [source2 ...] destination
Same as the previous syntax, but specifying the directory first, and the source file(s) last:
mv [options] -t destination source [source2 ...]
--backup[=vcm] Make a backup of each existing destination file, using the version control method vcm. If vcm is omitted, --backup behaves the same as -b (backups are created, using the default version control method). See Backing up files for details.
-b Like --backup, but does not accept a backup method. Instead, the method specified by the VERSION_CONTROL environment variable is used. Simple backups are created if the variable is not set. See version control methods for details.
-f, --force Always overwrite existing files without prompting. This can be useful if you need to overwrite multiple files whose permissions are read-only; if you don't specify -f, you will be prompted for every file.
-i, --interactive Prompt before overwriting an existing file, regardless of the file's permissions.
-n, --no-clobber Never overwrite any existing file.
Note: If you specify more than one of the above options -i, -f, or -n, only the final option you specify takes effect.
--strip-trailing-slashes Remove any trailing slashes from each source argument.
-S, --suffix=suffix Specify the file name suffix to be used for all backup files. The default is "~".
-t, --target-directory=destination Move all sources into the directory destination.
-T, --no-target-directory Treat destination as a normal file, not as a directory.
-u, --update Don't overwrite files if they're newer. A move will only happen if the destination file is older than the source file, or the destination file does not already exist.
-v, --verbose Provide verbose output. Print the name of every file moved.
--help Display a help message, and exit.
--version Display version information, and exit.
Backing up files
If you use the -b or --backup options, mv will rename the destination file if it exists, appending a suffix to its file name. This saves a copy of the original file instead of overwriting it.
There are two types of backups: simple and numbered.
Simple backups delete an existing backup file if it already exists. Only one backup file is kept. The default suffix for "simple" backups is a tilde ("~"). You can change this suffix with the --suffix option, or by setting the SIMPLE_BACKUP_SUFFIX environment variable. For example, file.txt would be backed up as file.txt~.
Numbered backups keep existing backup files, creating additional backups with an incrementing number in the file name. No backup files are deleted. The suffix for numbered backups is ".~n~", where n is an integer. For example, file.txt would be backed up as file.txt.~1~, then file.txt.~2~, etc.
Version control methods
Additional rules for creating backup files are available, called version control methods. The version control method may be specified with the --backup option, or by setting the environment variable, VERSION_CONTROL. The methods are:
Method name Description
none, off Never make backups, even if the --backup option is given.
numbered, t Make numbered backups.
existing, nil numbered if numbered backups already exist, simple otherwise.
simple, never Always make simple backups.
mv myfile.txt myfiles
Move the file myfile.txt into the directory myfiles. If myfiles is a file, it will be overwritten. If the file is marked as read-only, but you own the file, you will be prompted before overwriting it.
mv myfiles myfiles2
If myfiles is a file or directory, and myfiles2 is a directory, move myfiles into myfiles2. If myfiles2 does not exist, the file or directory myfiles is renamed myfiles2.
mv myfile.txt ../
Move the file myfile.txt into the parent directory of the current directory.
mv -t myfiles myfile1 myfile2
Move the files myfile1 and myfile2 into the directory myfiles.
mv myfile1 myfile2 myfiles
Same as the previous command.
mv -n file file2
If file2 exists and is a directory, file is moved into it. If file2 does not exist, file is renamed file2. If file2 exists and is a file, nothing happens.
mv -f file file2
If file2 exists and is a file, it will be overwritten.
mv -i file file2
If file2 exists and is a file, a prompt is given:
mv: overwrite 'file2'?
Entering "y", "yes", "Yes", or "Y" will result in the file being overwritten. Any other input will skip the file.
mv -fi file file2
Same as mv -i. Prompt before overwriting. The f option is ignored.
mv -if file file2
Same as mv -f. Overwrite with no prompt. the i option is ignored.
mv My\ file.txt My\ file\ 2.txt
Rename the file "My file.txt" to "My file 2.txt". Here, the spaces in the file name are escaped, protecting them from being interpreted as part of the command.
mv "My file.txt" "My file 2.txt"
Same as the previous command.
mv "My file.txt" myfiles
The result of this command:
If myfiles a directory, My file.txt is moved into myfiles.
If myfiles a file, My file.txt is renamed myfiles, and the original myfiles is overwritten.
If myfiles does not exist, My file.txt is renamed myfiles.
mv My*.txt myfiles
Here, * is a wildcard meaning "any number, including zero, of any character."
If myfiles is a directory: all files with the extension .txt, whose name begins with My, will be moved into myfiles.
If myfiles does not exist or is not a directory, mv reports an error and does nothing.
my My\ file??.txt myfiles
Here, ? is a wildcard that means "zero or one of any character." It's used twice, so it can match a maximum of two characters.
If myfiles is a directory: any file with zero, one, or two characters between My file and .txt in their name is moved into myfiles.
If myfiles doesn't exist, or is not a directory, mv reports an error and does nothing.
Tip: For more information about how wildcards are used in commands, see pathname expansion in bash.
mv -b file file2
If file2 exists, it will be renamed to file2~.
mv -b --suffix=.bak file file2
If file2 exists, it will be renamed to file2.bak.
mv --backup=numbered; mv file file2
If file2 exists, it will be renamed file2.~1~. If file2.~1~ exists, it will be renamed file2.~2~, etc.
VERSION_CONTROL=numbered mv -b file file2
Same as previous command. The environment variable is defined for this command only.
export VERSION_CONTROL=numbered; mv -b file file2
By exporting the VERSION_CONTROL environment variable, all mv -b commands for the current session will use numbered backups.
export VERSION_CONTROL=numbered; mv file file2
Even though the VERSION_CONTROL variable is set, no backups are created because -b (or --backup) was not specified. If file2 exists, it is overwritten.
Renaming files using regular expressions
mv does not interpret regular expressions (regex).
If you need to rename many files, using a complex or nuanced mapping from old to new file names, you should use the rename command instead.
rename accepts perl regular expressions. For example:
rename 's/My\ file(..)/document$1/' My*
This command will rename files My file.txt and My file 2.txt to document.txt and document 2.txt.