mv Command in Linux {With Examples}

October 24, 2023

Introduction

The mv command is a UNIX utility for renaming and relocating files and directories in a filesystem. While desktop operating systems also offer a GUI method for file manipulation, there are cases in which using mv in a terminal can be a more efficient solution.

This article shows you how to use the mv command in Linux, provides a list of options, and offers illustrative examples.

The mv command in Linux.

Prerequisites

mv Command Syntax

The mv command works with files and directories, yielding different results depending on the used arguments. There are four basic ways to execute an mv command:

  • Rename a file. Type the source filename and the destination filename:
mv [options] [source-file] [destination-file]
  • Rename (or move) a directory. Type the source directory and the destination directory. If a destination directory exists, mv moves the source directory to the specified destination.
mv [options] [source-directory] [destination-directory]
  • Move a file, preserving the filename. Type the filename followed by the destination directory.
mv [options] [file] [destination-directory]
  • Move a file and change its name. Type the filename followed by the destination directory and a new filename.
mv [options] [file1] [directory/file2]

mv Command Options

According to the POSIX standard, the mv command has only two options: -f (force remove) and -i (interactive mode). However, depending on the OS and the version of the mv command, more useful options are available as arguments.

Below is a list of the most common mv options.

OptionDescription
-b, --backupCreate a backup of files that will be overwritten or removed.
-f, --forceOverwrite destination files without prompting the user.
-i, --interactivePrompt the user to confirm if the mv action should overwrite a file.
-S, --suffix=Provide a suffix for a backup file. The default suffix is ~.
-u, --updatePerform the mv action only if the source file is newer or the destination file does not exist.
-v, --verboseShow an output describing the action taken.
--helpOutput the command help and exit.
--versionShow the command version and exit.

mv Command Examples

As seen above, the mv command is a simple but versatile file management utility. The sections below provide examples of the most common mv operations.

Rename File

Rename the file name1 to name2 with the following command:

mv name1 name2

The command produces no output, but the ls command shows the operation was successful.

Renaming a file in Linux with the mv command.

View Command Output

Rename the file name1 to name2 and print the output:

mv -v name1 name2

The command produces an output containing the list of the performed actions.

Showing verbose output for the mv command.

Move Multiple Files

Move the files name1, name2, and name3 to dir2 in the home directory and show the verbose output:

mv -v name1 name2 name3 test-dir

The output shows that the files have been moved successfully.

Renaming multiple files.

Ask Before Overwriting

Rename name1 to name2, but show a prompt to overwrite if the file name2 already exists.

mv -v -i name1 name2

In the example below, the file name2 was already in the directory. The mv command prompted to overwrite the file and replaced its contents with the contents of name1.

Prompting to overwrite a file.

Note: If you do not provide the -i option, mv assumes you want to overwrite the destination file.

Create File Backup

Move the file name1 to the directory test-dir. If the file of the same name exists at the destination, create a backup with the extension .bak:

mv -S .bak -b name1 test-dir/name1

After the command executes, the directory contains two files:

  • name1 is the moved file.
  • name1.bak is a backup of the file initially present in the directory.
Creating a backup of the overwritten file.

Batch Rename Files

Combine mv with the Bash for loop to rename multiple files in one command. For example, to add the prefix test_ to all the files in a directory, type:

for file in *; do mv "$file" "test_$file"; done

Listing the directory contents shows that all the filenames start with test_:

Renaming files in batch with for loop and the mv command.

Conclusion

After reading this article, you should know how to use the mv command to move and rename files and directories in Linux. The article included the most common command options and provided examples of the frequently performed operations.

To learn more about Linux CLI fundamentals, read our ultimate list of Linux Commands All Users Should Know.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
How to Use Linux Cat Command
July 13, 2020

The cat command, short for concatenate, is used to display the contents of one or more files, without having to open the file for editing.
Read more
How to Delete a File in Linux
September 7, 2023

Deleting unnecessary files in Linux is a routine task and is essential for maintaining a clean and functional system. Find out how to remove files in Linux using...
Read more
How to Copy Files and Directories in Linux
December 28, 2023

This guide will show you how to copy files and directories in Linux by executing CLI commands. Create system-wide backups or filter out and copy only...
Read more
How to Create a File in Linux
November 7, 2023

Creating a new file in Linux is straightforward, but there are also some surprising and clever techniques. In this tutorial learn how to to create a file from a Linux terminal.
Read more