MV command is a command that similar to cp command, but it does not create a copy/duplicate of files/directories. When you want to move files from one place to another and you don’t want to duplicate it, we use mv command.
This command is installed by default on your Linux system, in any kind of Linux you are using. Please take a look of some examples of using mv command in day-to-day operation.
1) Moving files
The requirement of moving file is the file source location must be different with the files destination location. Here’s an example. To move file_1.txt from current directory to another directory , for example /home/pungki/office, here’s the syntax :
$ mv file_1.txt /home/pungki/office
As we can see, when we move the file_1.txt, the file_1.txt from previous directory is deleted.
2) Moving multiple files
If we want to move multiple files, we put them in one line separated by space.
$ mv file_2.txt file_3.txt file_4.txt /home/pungki/office
You can also use a pattern if your files have it. For example, to move all files which have .txt extension, we can use this command :
$ mv *.txt /home/pungki/office
3) Moving directory
Different from the copy command, moving directory using mv command is pretty straightforward. To move a directory, you can just to use mv command without any options. Please take a look screenshot below.
4) Renaming files or directory
We also use mv command to rename files and directory. But in order to do so, the destination location must be the same as the source location. Then the file name must be different.
Let say we are inside /home/pungki/Documents folder and we want to rename file_1.txt into file_2.txt. Then the command will be like :
$ mv file_1.txt file_2.txt
If we mention the absolute path, then it will look like this :
$ mv /home/pungki/Documents/file_1.txt /home/pungki/Documents/file_2.txt
5) Renaming directory
The above rule is also applied to a directory as well. Take a look at this example :
$ mv directory_1/ directory_2/
6) Print what happen
When you are moving or renaming a large number of file/directory, you may want to know does your command works successfully or not without seeing to the destination location. To do this, we can use -v option.
For example, we want to move all txt files and want to check it. Then the command will be like this.
$ mv -v *.txt /home/pungki/office
The same way is applied to the directory.
7) Using interactive mode
When you are moving file into another location, and there is already exist the same file, then by default mv will overwrite it. No pop-up notification for this. To make a notification for overwriting file, we can use -i option.
Let say we want to move file_1.txt to /home/pungki/office. Meanwhile, file_1.txt is already existed in /home/pungki/office directory.
$ mv -i file_1.txt /home/pungki/office
This notification will aware us about the existence of file_1.txt in the destination location. If we press “y” then the file will be moved, otherwise, it will not.
8) Using update option
While -i are notifying us about overwriting files, then -u option will perform update only if the source is newer than destination file. Let’s take a look example below.
We have file_1.txt and file_2.txt with this attributes :
- File_1.txt has 84 bytes file size and it last modified time is 12:00
- File_2.txt has 0 bytes file size and it last modified time is 11:59
We want to move them into /home/pungki/office directory. But in the destination location, we already have file_1.txt and file_2.txt.
We move file_1.txt and file_2.txt from current directory into /home/pungki/office using command :
$ mv -uv *.txt /home/pungki/office
As a result, we see those files are moved. That file is moved because their last modified timestamp is newer than the files in /home/pungki/office directory.
9) Do not overwrite any existing file
If -i options is asking us about overwriting files, than -n option will not allow us to overwrite any existing files.
Using example on point 8, if we change the option from -u to -n, combine with -v option, then we will see that there are no files moved into /home/pungki/office directory.
$ mv -vn *.txt /home/pungki/office
10) Create backup when copying
By default, moving files will overwrite the destination files if there are already exist before. But what happens if you are moving wrong files, and the destination files are already overwritten by the new ones? Is there a way to retrieve the old one? Yes there is. We can use -b option. -b option will make a backup of destination file before it overwritten by the new one. Once again, we will use the scenario from point 8 above.
$ mv -bv *.txt /home/pungki/office
As you can see on the screenshot, on the /home/pungki/office directory, we have a file named file_1.txt~ and file_2.txt~ . The tilde sign (~) means that those files are backup. We can see the attribute of them is older than file_1.txt and file_2.txt.
Moving file or directory also one of the basic commands in the Linux system. As usual, you can type man mv or mv --help to display its manual page to explore more detail.