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.
$ mv [options] [source file/dir] [destination]
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.
Option | Description |
-v | Displays information on the move process. |
-n | Avoids overwriting files. |
-i | Prompts before overwriting a file. |
-f | Forcefully overwrites existing files or directories. |
-b | Creates a backup of the file or directory before overwriting it. |
–help | Display the help page for the command. |
–version | Display 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 file1.txt
To rename the newly created file, run the mv command and add the new name you wish to use for your file.
$ mv file1.txt new1.txt $ ls
We’ve run the Linux ls command to display the available files and confirmed that we’ve managed to rename our 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.
$ mkdir -v Names Files
We’ve created two directories and included the -v option for verbose.
To rename our directory, run the mv command as follows.
$ mv Names/ Renamed1
Also, run the ls command to verify that the directory has been renamed.
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.
$ mv -v new1.txt Files/
Notice how, after running the ls command, the file doesn’t exist as we’ve moved it.
However, when we run the ls command in the destination directory, we confirm that our file has been moved successfully.
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.
$ mv -v file1 file2 file3 Hosts/
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.
$ mv -v Renamaed1/ Files/
The ls command verifies that the directory has been moved successfully.
If you have multiple directories, add them to the same command and specify the destination at the end, as demonstrated below.
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.
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.
$ mv -iv file2 Hosts/
The example below shows how we get prompted to confirm overwriting the existing file.
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 -vb file1 Hosts/
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.
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 -fv file1 file2 Hosts/
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.