mv Command in Linux


Files and directories are the main part of an operating system. We often move files or rename them to keep things organized. While we can do this with a mouse or graphics, however, it is more convenient to use the mv command and do it directly through the terminal.

The mv command in Linux, which also applies to other UNIX-based systems like macOS, is used to move files or directories within the file system. It can also rename files or directories.

This article explains how to use the mv command in Linux with the help of various examples. We will also discuss its options and syntax in detail.

Table of Contents:

mv Command in Linux

The mv command performs two main tasks, to move a file or directory to a separate location, and to rename files and directories.

Syntax

The mv command in Linux has a similar syntax structure to that of the other commands. It has two main parts: options and arguments.

mv [options] <source_file/directory> <destination_file/directory>

Here, the source_file is the file name that you want to rename or move. While the destination_file is the new name of the file/directory. Or it is the new location to move the file to.

Note: As mentioned this command can also rename the files so don’t get confused with the above explanation. It all depends on what option you choose with the mv command. When you write this command without any path it renames the files, while on specifying the path it moves those files.

mv Command Options

You can use multiple mv command options on Linux. These options are integrated into the command’s basic syntax to manage its behavior. For example, you can specify if you want to preserve the original file timestamps or overwrite files without prompting the user.

The below table displays the list of the most common options available for the mv command:

Option Description
-b Create a copy of any files that might be replaced or deleted.
-i (interactive mode) Prompt you for confirmation before the mv command replaces an existing file.
-u (Updates) Execute the mv command to transfer a file only when no file exists at the destination.
-v (Verbose) Display an output message explaining the action that was performed.
-f (Force) Overwrite the files at the destination automatically, without prompting your confirmation.
-t (Target directory) Transfer all arguments of the source into a directory.
-s (Suffix) Provide a suffix while copying a file. The default suffix is ~.
-n (No-clobber) Used when you don’t need to overwrite a file that is present already.
–strip-trailing-slashes Eliminate the trailing slashes from the end of each source parameter.
–help It displays information related to commands.
–version Display the command version and exit.
–backup Create existing destination file backups.

You can get more information related to the mv command using the –help option:

mv --help

Additionally, you can also display the version of mv that is running on your system using this:

mv --version

Before we can proceed with the other operations related to this command, first create some sample files:

touch file.txt

Here we have created a new text file in the home directory.

Renaming a File or Directory in Linux

To rename a file using mv, type the source file name along with the destination file.

For example, to change the name file.txt to newfile.txt, we use the following command:

mv file.txt newfile.txt

If the filename newfile already exists, it will be overwritten without any warning confirmation. To rename a directory, simply use the directory name instead of the file name, as shown above.

Moving Single and Multiple Files or Directories in Linux

You can also transfer single or multiple files in Linux. To move a single file, simply enter the original file name path and your targeted path where you want to save your file.

Let’s move the file from path /home/linux/file.txt to the specified path /home/linux/Downloads:

mv /home/linux/file.txt /home/linux/Downloads

Furthermore, we can also move multiple file names using this mv command to a particular location:

mv /home/linux/Downloads/file.txt /home/linux/Downloads/file1.txt /home/linux/

Here, in this command, the files named file.txt and file1.txt have been moved to a particular destination path.

Displaying Output Message While Moving or Renaming File/Directory

You can also view the output message when you rename or move the file. This is done by using the -v option of the mv command. This option retrieves the output message displaying that the particular action was performed on the file or directory.

Let’s take this command as an example:

mv -v file.txt newfile.txt

Now you can see from the output message that the file is renamed.

Avoid Overwriting Files in Linux

The mv command will overwrite/modify the target file by default. To prevent data loss and avoid overwriting files, you can use the -n option. This option disables the default behavior of this command.

To understand this, let’s consider we have a file named newfile.txt, and we need to overwrite it, then we can run this command:

mv -v -n file.txt newfile.txt

You can see in the terminal as the file at destination already exists with the same name, so the file newfile.txt was not overwritten.

Note: One thing to consider is the -v option we used here. This option is used to display the action that is performed in the written form.

Overwriting Files After Confirmation

The -n option is used to disable file overwriting completely. However, if you want that before overwriting any file or directory, you see a confirmation message. For this, use the -i option with the mv command to do this.

This option will display a confirmation message and ask for permission whether you want to overwrite the file or not. This option activates the interactive mode, displaying a warning message on the terminal.

Now, let’s try to overwrite the newfile.txt in an interactive mode:

mv -v -i file.txt newfile.txt

Here, we can see the “mv: overwrite ‘newfile.txt’?” message. This message specifies that the command is waiting for user confirmation. To proceed with the operations, type y or for canceling type n.

Creating a Backup or Copy of the File Before Overwriting Files

In earlier examples, we examine how to safely overwrite the destination file. However, we can also use the mv command with a –backup option to set a backup policy. This option creates a backup of the destination file before it gets overwritten.

Let’s use the -b option with the mv command and create a duplicate copy of a file before it’s overwritten:

mv -v -b file.txt newfile.txt

From the output, see that we have renamed the file and also created a backup of the file. The backup file contains a tilde (~) at the end.

Conclusion

The mv Linux utility is an important aspect of terminal-based file management. It serves two purposes: it can rename files or directories and can also move them to different locations. The mv command has several options that can be used along with the basic mv syntax to perform several operations. These operations mainly include taking a backup of files, showing verbose output, and any others. The overall mv command is a basic Linux tool for organizing files in Linux.

Print Friendly, PDF & Email
Categories