The mv command is used in Linux to move or rename files and directories. When the mv command moves files it basically deletes the files from the source where cp command keeps a copy of the file.
By default, it will overwrite the file in the destination if it exists and will not show any prompt for confirmation.
The mv command-line utility is available by default on all Linux systems. In this article, we learn how to use mv command and its options with examples.
Basic syntax:
mv [option] source destination
1) Moving files
The requirement of moving files is, the file source location must be different with the destination location. You can use an absolute or relative file path.
To move file_1.txt from the current directory to another directory /home/pungki/office, use:
$ mv file_1.txt /home/pungki/office

As we can see, when we move the file_1.txt, the file_1.txt from the source directory is deleted.
An important thing to remember here, when moving a file to destination, if the destination already has a file with the same name then it will get overwritten.
2) Moving multiple files
To move multiple files, we put them in one line separated by space, as:
$ mv file_2.txt file_3.txt file_4.txt /home/pungki/office

You can use patterns to move files. For example, to move all files which have .txt extension, use:
$ mv *.txt /home/pungki/office

3) Move directory
To move a directory, use mv command following by source directory, then leave a space then destination directory. If the destination already has the same directory then it will overwrite.

4) Rename file or directory
To rename a file or directory, the destination location must be the same as the source location. The file name must be different.
Let say we are inside /home/pungki/Documents folder and want to rename file_1.txt to file_2.txt, use:
$ 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

To rename directories, the above same rule is applied as well.
Example:
$ mv directory_1/ directory_2/

5) Prompt before overwrite
When you are moving a 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 make us aware of the existence of file_1.txt in the destination location. If we press “y” then the file will be moved, otherwise, it will not.
6) Move only when file newer than destination
While -i are notifying us about overwriting files, then -u option will perform the update only if the source is newer than the destination file.

We have file_1.txt and file_2.txt with these file timestamps:
- 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.
Move file_1.txt and file_2.txt from the current directory into /home/pungki/office, use:
$ mv -uv *.txt /home/pungki/office
The files moved because their last modified timestamp is newer than the files in /home/pungki/office directory.
7) Do not overwrite an existing file
If -i options is asking us about overwriting files, than -n option will not allow us to overwrite any existing files.
Example:
If we change the option from -u to -n, then we will see that there are no files moved into /home/pungki/office directory.
$ mv -vn *.txt /home/pungki/office

8) 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. The -b option will make a backup of the destination file before it overwritten by the new one.
$ 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.
9) Verbose - Print what is being done
Use -v option to display what mv command does when executing the command.
Move all txt files and want to check verbose, use:
$ mv -v *.txt /home/pungki/office

Conclusion
Moving files or directories is 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.
Does mv -f command changes record format?
-f option will force overwrite destination file without prompting