Mv Command in Linux with Examples

Mv Command in Linux

Mv command is used to move files and directories in Linux.

This article focuses on some examples of mv command in Linux. We have used RHEL 9 for demonstration.

Syntax

The command has following syntax.

mv [OPTION]. . . SOURCE DESTINATION

Examples of mv Command in Linux

Move file or directory to another directory

The first example illustrates how can you move a file or directory to some other directory. Suppose the file is sample1.txt and it is located in your current directory. You want to move it to another directory i-e backup/. You would run:

mv sample1.txt backup/

If the file is already located at the destination, it will be overwritten.

Similarly, you can move the directory from one location to another. Suppose you want to move a directory new_directory from your current directory to backup/ directory. You should run the command as follows:

mv new_directory backup/

Move multiple files to another directory

Suppose you want to move multiple files (sample2.txt, sample3.txt) to another directory i-e backup/. You should run the following command.

mv sample2.txt sample3.txt backup/

Rename a file or directory

With the help of mv command you can rename the files as follows.

mv old_file.txt backup/new_file.txt

Similarly, you can rename the directories as follows.

mv old_directory backup/new_directory

Ask before replacing the file or directory

You can make an mv to ask for permission before overwriting the file (if the file is already located at the destination) using -i option as follows.

mv -i sample_file.txt backup/

Print verbose output

While moving the file or directory, you can make the mv command print the action being taken with file or directory as follows:

mv -v sample_file.txt backup/

Do not overwrite the file

As we have said, if the same file is already located at the destination, it is overwritten by the new file. If you want that the file shouldn’t be overwritten, use the -n option as follows.

mv -n sample_file.txt backup/

Conclusion

Thank you. Use man mv on the terminal to know more about this command.