1. Overview

In Linux, the mv command moves files and directories from one location to another. Additionally, we can use it to rename files and directories.

In this tutorial, we’ll discuss the various functionalities of the mv command using detailed examples.

2. Basic Usage

mv uses the following command syntax:

mv [options] source destination

Let’s go over the arguments shown above:

  • [options] – represent optional flags used to modify the behavior of the mv command
  • source – represents the file or directory we want to move, which we can specify using either a relative or absolute path
  • destination – specifies the location where we want to move the source file or directory

Next, let’s explore various functions of mv.

2.1. Moving Files

Let’s move a file from one location to another:

$ mv fruits.txt /home/samuel/Desktop

Above, we move the fruits.txt file from its current location to the /home/samuel/Desktop directory. Additionally, if a file with the same name exists in the destination directory, it will be overwritten.

Furthermore, we can move multiple files at the same time:

$ mv report.pdf tenants.csv credentials.csv /home/samuel/Desktop

The above command moves the report.pdf, tenants.csv, and credentials.csv files from the current working directory to the /home/samuel/Desktop directory simultaneously.

2.2. Moving Directories

We can also move a directory from one location to another:

$ mv Projects /home/samuel/Desktop

In the example above, we move the Projects directory with all of its contents from the current directory to the /home/samuel/Desktop directory.

Additionally, we can move multiple directories simultaneously:

$ mv Pdf Images /home/samuel/Downloads/

Here, we move the Pdf and Images directories from their current location to the /home/samuel/Downloads directory.

Unlike with moving files, when we try to move a directory and it already exists in the destination directory, we’ll get an error:

$ mv Coding_Projects /home/samuel/Desktop
mv: cannot move 'Coding_Projects' to '/home/samuel/Desktop/Coding_Projects': Directory not empty

Above, we get an error when moving the Coding_Projects directory to the /home/samuel/Desktop directory. This occurs because the Coding_Projects directory already exists in the destination and it’s not empty.

2.3. Renaming Files and Directories

We can also use the mv command to rename files and directories. To do this, we need to specify the current filename as the source and the new filename as the destination:

$ mv file1.txt cars.txt

Here, we rename the file1.txt file to cars.txt in the same directory.

We can also use the same approach to rename directories:

$ mv Projects Coding_Projects

The above command renames the Projects directory to Coding_Projects in the same directory.

What’s more, we can rename files and directories when we move them:

$ mv data.txt /home/samuel/Download/Updated_data.txt

In the example above, we move the data.txt file to the /home/samuel/Download directory and rename it to Updated_data.txt.

On the other hand, for directories, it works a bit differently:

$ mv Report /home/samuel/Documents/Project_Report

When we run the above command, if the Project_Report directory already exists, the Report directory will be moved into it. However, if the Project_Report directory doesn’t exist, the Reports directory will be renamed to Project_Report and moved to the /home/samuel/Documents/ directory.

3. Advanced Usage

Now, let’s explore some advanced usage scenarios of the mv command.

3.1. Prompt Before Overwriting

To prevent overwriting a file, we use the -i option. This option prompts us for confirmation before overwriting files in the destination directory:

$ mv -i tenants.csv /home/samuel/Desktop
mv: overwrite '/home/samuel/Desktop/tenants.csv'?

Above, we try to move the tenants.csv file from its current location to the /home/samuel/Desktop directory. Since there’s already a file with the same name in the destination directory, we get a prompt asking us if we want to overwrite it. We can input y to proceed or n to cancel.

3.2. Prevent Overwriting Existing Files

Using the -n option, we can prevent overwriting existing files in the destination directory:

$ mv -n Books.csv Employee_details.txt /home/samuel/Documents

In the example above, we move the Books.csv and Employee_details.txt files to the /home/samuel/Documents directory. But, if there are any files with the same names in the destination directory, they will not be overwritten. We keep the original files and don’t move the new files.

3.3. Verbose Output

When moving files or directories, we can use the -v option to display detailed information about the operation being performed:

$ mv -v credentials.csv /home/samuel/Download
renamed 'credentials.csv' -> '/home/samuel/Download/credentials.csv'

In the above command, we move the credentials.csv file from the current directory to the /home/samuel/Download directory. We then get feedback indicating the file has successfully been moved to the destination directory.

3.4. Moving Files Based on a Pattern

Using wildcards with the mv command, we can move files based on a pattern such as file extensions or parts of a filename. Wildcards are special characters representing one or more characters in a filename. We’ll use the asterisk (*) wildcard.

To demonstrate, let’s move all csv files to another location:

$ mv *.csv /home/samuel/Desktop

Here, we use the * wildcard to move all files with the .csv extension from the current directory to the /home/samuel/Desktop directory.

Furthermore, we can move files containing a specific prefix and extension:

$ mv results*.txt /home/samuel/Documents

In the example above, we move all the files in the current directory whose names start with results and end with .txt to the /home/samuel/Desktop directory.

Finally, let’s move files with the same file name but different extensions:

$ mv report.* /home/samuel/Downloads

Above, we move all files in the current directory that start with the name report and have any extension to the /home/samuel/Downloads directory.

4. Conclusion

In this article, we explored how to use the mv command through examples. We’ve seen how to use various options to customize the behavior of mv when moving files and directories.