How to Use the mv Command in Linux – 8 Examples

mv-command

Files and directories make up the Linux filesystem. The mv command helps with relocating files and directories for better organization. Moreover, you can utilize it to rename a file or directory.

When you use the cp command to copy a file or directory, a copy will be left in the source location. However, the mv command moves the file or directory, leaving no copy. That’s the key difference between the cp and mv commands in Linux. This post discusses the mv command and gives examples of using it.

How to use the mv Command in Linux

The first step is understanding the syntax when using the command to move or rename your files or directories.

The mv command preserves the filename unless you specify a different name. The table below presents some options you can use when running the command.

OptionDescription
-vDisplays information on the move process.
-nAvoids overwriting files.
-iPrompts before overwriting a file.
-fForcefully overwrites existing files or directories.
-bCreates a backup of the file or directory before overwriting it.
–helpDisplay the help page for the command.
–versionDisplay the version of the mv command.

mv Command Examples

Your goal determines what option, if any, to use when running the mv command. This section presents examples of using the mv command to move and rename files and directories.

1. Renaming a File using mv Command

When you run the mv command without specifying a destination path, the source file will get renamed and saved in the current working directory.

Let’s quickly run the Linux touch command to create a sample file.

touch-command

To rename the newly created file, run the mv command and add the new name you wish to use for your file.

We’ve run the Linux ls command to display the available files and confirmed that we’ve managed to rename our file.

mv-command-rename-file

2. mv Command Rename Directory

Like renaming a file, you can quickly rename a directory by specifying the new name. First, let’s create a directory using the mkdir command.

We’ve created two directories and included the -v option for verbose.

mkdir-command

To rename our directory, run the mv command as follows.

Also, run the ls command to verify that the directory has been renamed.

mv-command-rename-directory

3. Moving a File(s)

When you specify the destination, the mv command will move the selected file to the new location and save it using the same filename. However, you can specify a new name.

Here’s an example where we’ve moved a file to a directory in the same location.

Notice how, after running the ls command, the file doesn’t exist as we’ve moved it.

moving-a-file

However, when we run the ls command in the destination directory, we confirm that our file has been moved successfully.

verify-moved-file

What about moving multiple files? Suppose you want to move numerous files; the mv command has your back. Add all the target files and specify the path where to move them as the last argument in the command.

Here’s an example where all our files have been moved to the Hosts directory. Adding the -v option displays the information on the move process.

moving-multiple-files

4. Moving a Directory(s)

Moving a directory follows the same procedure as moving files. Add the source directory and the destination, and the move process will occur.

Here’s an example.

moving-a-directory

The ls command verifies that the directory has been moved successfully.

verify-moved-directory

If you have multiple directories, add them to the same command and specify the destination at the end, as demonstrated below.

moving-multiple-directories

5. mv Command Specify Not to Overwrite

When you run the mv command and the destination contains files with the same name as your source file, they will get overwritten. However, you can choose not to overwrite them by including the -n option in your command.

For instance, the below output shows that our mv command didn’t proceed as the destination has files with the same names.

mv-command-no-overwrite

6. How to Overwrite Interactively

Instead of overwriting the existing files without your consent, the better approach is to use the -i option, which will prompt you before overwriting.

The example below shows how we get prompted to confirm overwriting the existing file.

mv-command-overwrite-interactively

7. Creating a Backup for Existing Files

Another way to handle existing files is by creating a backup before you overwrite them. Assuming you must overwrite existing files, you can add the -b option to have a backup for all files and directories that will be overwritten.

mv-command-create-backup

When we list the available contents, we can see that our backups were created, and we managed to overwrite existing files while preserving the backups.

ls-command-verify-created-backup

8. Force Overwrite with mv Command

With the -f option, you can forcefully overwrite existing files even when you don’t have the required write permissions on the file or directory.

You won’t get any prompt when running this option.

mv-command-force-overwrite
Read Also: How to delete files and directories in Linux using the rm command.

Conclusion

Moving or renaming files and directories in Linux is best achieved using the mv command. This post discusses the command in detail and gives practical examples of how to use it.

Found this helpful? - Share it!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top