In Linux systems, moving of files and folders is one of the most common tasks performed when working on the command line. The mv command is a Linux move file command which has made the life easier for completing all the move-related operations. It is a command-line based utility for moving files and directories on Linux/Unix systems from a source location to the destination location. In most cases, it is just a single line command which makes much bigger tasks possible.
In this article, you are going to learn how to perform different types of interesting operations using the mv command.
Introduction to the Linux Move File Command (mv)
From the start, I will run its help command to make you more aware of it.
In Linux, the mv means to move. This command is used to move files or a group of files or directory from a source location to the destination location. It moves the exact content of the file(s) on a disk with your pre-defined different file name. The Linux move file (mv) command needs at least two file names in its arguments to operate. There are many types of utilization of move command, but I will show you the foremost and daily life practical commands which will help you in the workflow.
The base syntax for moving any file from a source address to the address is:
mv <source address> <destination address>
Now by further changing the arguments of the base syntax, I will be performing different Linux move file operations which you will see more in this article.
Move a File in the Same Directory
As said earlier, that the mv command helps the user in moving the content of a source file to its destination, so here I will be moving the content of a source file (demo) to the new destination file (ceos3c). If the destination file already exists, so this command will overwrite that file without any warning message, and if the destination file will not exists, then a new file will be created which will move the content of source file as per user’s desire.
mv demo ceos3c
Move a Single File to a Directory
By using this move command, you can move the contents of a source file (ceos3c) to the destination directory (newfolder) whenever needed. If the directory doesn’t exist then first, it will create a new directory and then it will move the source to it, but if the directory already exists then mv command will overwrite the pre-existing content in the destinated directory.
mv ceos3c newfolder/
Move Multiple Files to a Directory
With the help of this Linux move file command, you can move multiple files to a directory whenever needed. Suppose the various source files (ceos3c1, ceos3c2, ceos3c3) required to move all at once to a specific directory (newfolder) then you can frame the above command as shown. Using this command, the mv will move the entire content from the source file to the mentioned destinated directory. If the destination directory does not exist, then first it will create a new directory and will move the content to it but, if the directory already exists then, mv will overwrite all the pre-existing files in the destinated directory so be careful while moving the content from source to destination.
mv ceos3c1 ceos3c2 ceos3c3 newfolder/
Move source directory into the destination directory
This Linux move file command uses its recursive performance by replicating the entire source directory structure recursively to the destination location. It is used in a situation where you want to move all the files and sub-directories that a directory contains, so it will simplymove the whole directory instead only its files to the desired destination path. Even if the destination directory does not exist, it will create one, and if it would already exist, then it will replace and overwrite the existing file(s).
mv demo ceos3c
Interactive move command
Usually, when you use the move command and if the destination file already exists, it merely overwrites it. For making it interactive so that it will show a confirmation prompt while moving a file, the –i argument comes into the play. Using this argument, whenever you will move source (ceos3c) to the destination location and if a file with the same name would exist there then it will prompt to ask for the confirmation to overwrite the destination file (demo). If you answer the prompt as y (yes) then it will overwrite to the destination file or your answer would be n (no) then it will save the destination content from being erased.
mv -i ceos3c demo
Verbose move command
Sometimes when you perform a complex operation with the Linux move file command which includes multiple source files (ceos3c, ceos3c2, ceos3c3, ceos3c4) to the destination (newfolder) location so there would be several chances of typing errors or the wrong destination as it is a command-line based operation, then in this case you can use verbosity (-v). It will show you the detailed output of the actions that are performed, from where the source occurs and where it destinated.
mv -v ceos3c1 ceos3c2 ceos3c2 ceos3c3 newfolder/
Backing up a file while moving
There is a situation when you need to create a backup of the destination file (ceos3c1) while moving the content of the source file (ceos3c2) on it; then there is -b argument for this purpose. It helps in creating a backup of the file in the same folder with the different name and format.
mv -b ceos3c1 ceos3c2
Move using a Wildcard character
The star (*) wildcard character is used to select all or a group of the file concerning the way it could be used. As shown in the example below, I have used the source two ways to move all files (in a first screenshot) and a group of file (in the second screenshot). Firstly, I have moved all the files using an asterisk (*) character which has moved all the file regardless of their differences to the destination folder, while in a second way, I have moved all the files which have .txt extension irrespective of their names to the destination folder. And not just .txt, you can move the file of any extension you want in this way.
mv * newfolder/
mv *.txt newfolder/
Force move to a file
When a destination file permission does not allow to overwrite the data of source file on it, and it is needed to write the data on it forcefully, then in this case –f becomes handy. By using the –f (force) argument of Linux move file command, you can forcefully overwrite the destination file with the data of the source file. It deletes the destination file first and then moves the contents of the source file on it.
mv -f ceos3c.txt demo.txt
Move Command with Recency attribute
There could be scenarios when you only want to move the source files only if they are newer than the destination ones. This functionality can be easily be achieved by using -u of the Linux move file command. In the below-shown example, you can see that destination (demo) file was more recent than the source (ceos3c) file, so it did not change the contents of the destination file the -u command. You can manage the redundancy of the files, and the data loss can be prevented, which could occur by mistakenly overwriting the old data on the new one.
mv –u ceos3c demo
Conclusion of the Linux Move File Command
As you have seen till now that the usage of the mv command is straightforward and simple to understand as shown in the examples above that are covered in this tutorial. I hope it will help you in learning about the Linux move file command more effectively in more detail. I would recommend that you should try all of at least once to get the hands-on usage of it. That’s all from this article and please do share your feedback and comments.