Log In

Linux mv Command

Linux mv Command
21.02.2024
Reading time: 8 min
Hostman Team
Technical writer

Within any operating system, users have to move and rename various types of files to make them easier to organize. This is where the mv command, short for move, comes into play and allows you to interact with files in Linux. The versatility and usability of this command make it a choice not only for beginners, but also for advanced users.

In this article, you'll learn various functions of the mv command and understand its purpose in the Linux environment. In this tutorial we'll unleash useful tips on how to use this feature effectively. This guidance is specifically written in common language to help even novice users master the mv command and improve their file management skills.

Basic syntax and options of mv command in Linux

The basic syntax of the mv command is as follows: 

mv [option] [source] [destination]

where the source is the file or directory being moved, and the destination is the target location. Here you can indicate whether the source is a file or a directory.

The mv command in Linux is essential for renaming files or directories. To do this, simply indicate a new destination name.

For instance, the command below will rename the file 'file1.txt' to 'file2.txt':

mv file1.txt file2.txt

With the mv command you can move files between different file systems by indicating an absolute path to the destination. The option helps to move massive files to another location. The simple yet powerful mv command saves time and effort, especially while dealing with multiple items.

Options used with the Linux mv command

There are various options that can be used with the mv command to perform different tasks:

  1. -v (verbose) option shows the full information about the files being moved or renamed. It provides the user with detailed output of the operation, including the source and destination paths, as well as any errors that may have occurred. This option helps you track which files are being moved and where they are going;

  2.  -i (interactive) option prompts the user for confirmation before overwriting an existing file. It helps prevent accidental overwriting of important files. This function allows you to review and approve each operation before it is performed;

  3.  -f (force) option forces the move or rename operation, even if it results in overwriting an existing file. This feature is not safe if used carelessly, as improper use may result in the loss of important data. However, in some cases this option may be useful if you want to replace an existing file with a new one without asking for consent;

  4.  -u (update) option moves or renames the source only if it is newer than the destination. If the source file is older than the destination file, the operation will not be executed, and the destination file will remain unchanged. This feature helps you update a file without losing any changes made to the destination file;

  5.  -b (backup) option creates a backup of the existing file before overwriting it. This feature helps you preserve the initial file in case the move or rename operation fails. The backup file will have a ~ appended to its name, making it easy to identify.

How to use mv command in Linux

Most often Linux mv command is utilized to move a file from one place to another. To do this, you should indicate the source file and the destination directory. For instance, to move a file named 'report.txt' from the current directory to the Documents directory, apply the command:

mv report.txt Documents

The file will be relocated to a new directory and removed from its current storage.

The mv command also allows users to move or rename not only one file, but also multiple items at once in Linux.

For instance, to rename a set of files with the extension '.doc' to '.txt', apply the command:

mv *.doc *.txt

With the mv command you can also relocate entire directories by indicating their names as the sources and the new locations as the destinations. This solves the problem of storing large directories or moving them to another place in the operating system.

Another way to utilize the Linux mv command is to move files from one directory to another, especially to store files in different folders. For instance, to relocate a concrete document from a folder to a subfolder, apply the mv command to complete the process. Read on to learn more about the capabilities of the mv command.

Additionally, you can use the mv command to move all files and subdirectories from the 'Documents' folder to the 'Downloads' folder. Let’s look at the following example:

mv ~/Documents/* ~/Downloads

The asterisk (*) wildcard character represents all files and directories in the 'Documents' folder. By applying this feature, we give a command to move all items inside the folder instead of indicating each individual file or directory. This saves time, especially if there are numerous items in the Documents folder.

After the process is completed, all files and subdirectories will be moved to the 'Downloads' folder, allowing you to efficiently organize and store the files. This will also bring order to the operating system and make it easier to find and access the necessary files in the future.

Be cautious while using the mv command as it will overwrite existing files in the destination folder if they have the same name as the files being moved. Therefore, you should check twice the command before action to avoid accidentally losing a file.

How to move a single file using the mv command in Linux

In this section, we will unleash step by step what exactly you need to do to move a single item from one location to another with the help of this feature. So, you already know that the mv command in Linux allows you to move a file from one location to another, no matter whether it is in the same directory or to a completely different placement. This function helps optimize the files storage or organize their movement between different folders.

To get started, open the command line interface and proceed to the directory with the file to be moved. As soon as you are in the right location, apply the mv command to move the file.

Follow the syntax for the action: 

mv [source] [destination]

where the source is the file to be moved, and the destination is the place where it should be relocated to.

For instance, to relocate a file named 'report.txt' from the current directory to a folder called 'Documents', apply the command: 

mv report.txt Documents 

The item will be transferred from its initial location to the Documents folder.

If you need to relocate a single item to a directory in Linux outside of your current one, you should indicate the full path to the destination with the help of mv command. For instance, to move the file to a folder called 'Backup' in your home directory, apply the command:

mv report.txt /home/username/Backup

If you wish, you can also rename the file while moving it, giving it a new name at the destination.

For instance, to rename 'report.txt' to 'monthly_report.txt' while transferring it to the Documents folder, apply the command: 

mv report.txt Documents/monthly_report.txt

An important addition is that the mv command will overwrite any existing file with an identical name in the destination. So keep in mind that if you move a file to a folder in Linux where a file with the same name is stored, the old item will be replaced by the new one by applying the mv command. 

With its simple syntax and the ability to transfer any single file to the desired placement, the mv command is a working tool that should be in every Linux user's arsenal.

How to move multiple files and directories with the mv command in Linux

The mv command is often used to relocate multiple files and directories in Linux at once. In this section we will show you in detail how to do this. 

To relocate multiple items with the help of the mv command, you would use the same basic syntax of the command: 

mv [source] [destination]

The source is a file or directory being moved, and the destination is the target location.

The destination must be an existing directory, otherwise the process will fail. To move multiple files, you should list them all after mv and before the destination.

For instance, the following command will transfer all three files to the indicated placement:

mv file1.txt file2.txt file3.txt destination/

Another way to transfer multiple files and directories in Linux is using wildcards in mv command, which are characters representing a group of files or directories. For instance, the asterisk (*) shows any number of characters in a file or directory name. To transfer all files that start with 'file', apply the command:

mv file* destination/

Also you can utilize the question mark (?) wildcard to represent a single character when moving files with similar names, such as 'file1.txt' and 'file2.txt'. The command below will relocate both files to the specified destination.

mv file?.txt destination/

Conclusion

The versatile mv command in Linux allows users to easily move and rename files and directories, effectively managing and organizing them properly. Its ability to move files and directories between different locations in the system helps transfer the items between folders or directories. It also allows you to rename files without creating a new one.

Additionally, the mv command allows you to move and rename multiple files at once to save your time and effort. Its multi-functionality offers a simple and efficient way to store and manage files and directories. Understanding the correct use of the mv command allows you to control your files and directories in Linux.

Share