×


Mv Command in Linux - Explained with Examples

The mv command stands for move which allows the user to move one or more files or files at once from one place to another in the file system. It can also rename files and folders and this command works without a confirmation message.

This command is built-in on Linux distributions.

Here at Ibmi Media, we shall look into how to use the mv command in Linux.


What is the syntax of mv command ?

It is given below:

$ mv [option] source destination


mv command Options:

1. -i (interactive): show overwrite confirmation message.

Press y to yes, n to no. 

2. -n (no-clobber): prevent overwriting.

With this option, mv prevent overwriting of file.

3. -b (backup): back up an existing file.

The backup file will appear with the character (~) appended.

4. –version: check your version:

$ mv --version


Consider the below example

If you have the following file in a directory,

test1.txt
test2.txt
test3.txt

You can list them with the ls command:

$ ls

In order to rename the file test3.txt to mv.txt, apply the below command:

$ mv test3.txt mv.txt

Note: Be careful not to rename the file with an existing file because it will overwrite without notice.


[Need help in fixing Linux system issues ? We can help you. ]


Conclusion

This article covers how to use the mv command in Linux.


mv command Options

  • --backup[=vcm]: Make a backup of each existing destination file, using the version control method vcm. If vcm is omitted, --backup behaves the same as -b (backups are created, using the default version control method). See backing up files for details.
  • -b: Like --backup, but does not accept a backup method. Instead, the method specified by the VERSION_CONTROL environment variable is used. Simple backups are created if the variable is not set. See version control methods for details.
  • -f, --force: Always overwrite existing files without prompting. This can be useful if you need to overwrite multiple files whose permissions are read-only; if you don't specify -f, you are prompted for every file.
  • -i, --interactive: Prompt before overwriting an existing file, regardless of the file's permissions.
  • -n, --no-clobber: Never overwrite any existing file.
  • --strip-trailing-slashes: Remove any trailing slashes from each source argument.
  • -S, --suffix=suffix: Specify the file name suffix to be used for all backup files. The default is "~".
  • -t,
  • --target-directory=destination: Move all sources into the directory destination.
  • -T, --no-target-directory: Treat destination as a normal file, not as a directory.
  • -u, --update: Don't overwrite files if they're newer. A move only happens if the destination file is older than the source file, or the destination file does not already exist.
  • -v, --verbose: Provide verbose output. Print the name of every file moved.
  • --help: Display a help message, and exit.
  • --version: Display version information, and exit.