mv command is used to move files from one directory to other directory. Also mv command is used to rename file in Linux systems. It is an frequently uses command by the Linux users. You must learn about mv command in Linux and its parameters.
In this tutorial you will learn Linux mv command with useful examples
Syntax:
Usage: mv [OPTION]... [-T] SOURCE DEST or: mv [OPTION]... SOURCE... DIRECTORY or: mv [OPTION]... -t DIRECTORY SOURCE...
The move command is useful to rename SOURCE file to DEST file name. Also you can move a SOURCE file to other DIRECTORY. This command also allowed us to move DIRECTORY including all subdirectories to target directory.
mv Command Examples
Below is the basic mv command examples on Linux terminal.
- Rename file in current directory – For example rename file source.txt to dest.txt in current directory.
mv source.txt dest.txt
- Move file to destination directory – Move a source.txt file available in current directory to /tmp directory.
mv source.txt /tmp/
In above command the file name is unchanged at target directory. You can also rename file name at destination directory just by providing filename at destination. Like:
mv source.txt /tmp/dest.txt
- Move file from source directory to destination directory – We can also move files or directory from some other directory to destination directory.
The below command will move /var/dir1/source.log file to /var/log directory with same name.
mv /var/dir1/source.log /var/log/
- Move multiple files to destination at once – The mv accepts multiple source files and move them to the destination directory at once.
The following command will move file1.txt, file2.txt in current directory and /opt/file3.txt to the /tmp directory. Here the last command line parameter is used as destination by the mv command.
mv file1.txt file2.txt /opt/file3.txt /tmp
You can also use
-t
option to provide destination directory.mv -t /tmp file1.txt file2.txt /opt/file3.txt
mv Command Line Options
You must remember some of the command line options for mv command useful while working on terminal.
-b
Use this option to create a backup of each existing destination file. This is very useful from unwanted overwritten and data loss. If the destination file already exists it create a copy of destination file with ~ appended in file name.mv -b source.txt dest.txt
-f, --force
Use this option to overwrite destination file without prompting for the confirmation. This is useful for using mv command in automation tasks or shell scripts.mv -f source.txt dest.txt
-i, --interactive
This option is used to always prompt for confirmation before overwriting destination file.mv -i source.txt dest.txt
cp: overwrite 'dest'?Enter y/n option to allow or deny overwrite request.
-t, --target-directory=DIRECTORY
Use this option to move multiple files to destination directory.mv -t /tmp file1.txt file2.txt /opt/file3.txt
The above command will move file1.txt, file2.txt and /opt/file3.txt files under the /tmp directory.
-T, --no-target-directory
If a directory at destination with same name already exists, then default command moves source file under the destination directory. This option instruct mv command to treat destination as file and stop moving source under the destination.mv -T source.txt output
mv: cannot overwrite directory 'output' with non-directory-u, --update
– This option tells mv command to move only if SOURCE file is newer than the destination file or the destination file is missing.mv -u source.txt dest.txt
Conclusion
In this tutorial, you have learned uses of Linux mv command with the useful examples and command line options.
Please do share you thoughts about this article via comments to improve it. Your