On Linux operating systems, the mv command moves and renames files and directories.

Syntax

Rename a file named source to destination:

  • Syntax

  • Options

  • Backing up files

  • Version control methods

  • Examples

  • Making backups

  • Renaming using regular expressions

  • Related commands

  • Linux commands help

  • Options

  • Version control methods

  • Making backups

  • Renaming using regular expressions

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 …]

Options

Backing up files

If you use the -b or –backup options, mv renames 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.

If you specify more than one of the options above, only the final option you specify takes effect.

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:

Examples

mv myfile.txt myfiles

Move the file myfile.txt into the directory myfiles. If myfiles is a file, it is overwritten. If the file is marked as read-only, but you own the file, you are 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 to the parent directory of the current directory.

mv -t myfiles myfile1 myfile2

Move the files myfile1 and myfile2 to 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 is 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” results in the file being overwritten. Any other input skips 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”

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, are moved to 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 meaning “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.

Making backups

mv -b file file2

If file2 exists, it is renamed to file2~.

For more information about how wildcards are used in commands, see pathname expansion in bash.

mv -b –suffix=.bak file file2

If file2 exists, it is renamed to file2.bak.

mv –backup=numbered; mv file file2

If file2 exists, it is renamed file2.~1~. If file2.~1~ exists, it is 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 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 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 use the rename command instead.

rename accepts perl regular expressions. For example:

rename ’s/My\ file(..)/document$1/’ My*

This command renames files My file.txt and My file 2.txt to document.txt and document 2.txt.

cp — Copy files and directories.ln — Create a link, or a symbolic link, to a file or directory.rename — Renames multiple files using a regular expression.rm — Remove a directory.