mv Linux Command
mv Linux Command
The mv command in Linux is used to move or rename files and directories. It can relocate files from one directory to another or simply change their names without making a copy. The mv Linux command moves files and directories from one place to another on the Linux system.
mv Command Syntax
The general syntax of the command is as follows:
$ mv [options] source destination
- source – The file or directory you want to move or rename.
- destination – The new location or new name for the source file/directory.
Common Options
- -i : Interactive mode. Prompts before overwriting a file.
- -f : Force move without asking for confirmation.
- -v : Verbose mode. Shows details of the move operation.
- -n : No overwrite. Prevents replacing existing files.
Example
Move a file to another location
Let’s understand the move command with a simple example. The below command moves the sample.txt from the current location file to the /etc/cloud directory.
$ sudo mv sample.txt /etc/cloud
Another Example
$ mv tdocs.txt /home/testingdocs/Documents/
The above command moves tdocs.txt
to the /home/testingdocs/Documents/
directory.
Move a folder to another
$ sudo mv jdk16 /opt/jdk
Here in this example, we are moving the jdk16 directory contents to /opt/jdk. The source is jdk16 directory. The destination is /opt/jdk
To know more about the command, visit the man page.
$ man mv
In the example, we have also used the sudo command. To know more about sudo command:
Renaming a File
You can also rename a file by moving it to a new file name in the same directory:
$ mv oldname.txt newname.txt
Using Wildcards
You can move multiple files using wildcards (*), for example:
$ mv *.txt /path/to/destination/
This moves all .txt files from the current directory to the destination directory.
Overwriting Without Prompting (Force Option)
If the destination already exists, the mv command will overwrite the destination file without asking. To prevent this and prompt for confirmation, use the -i (interactive) option:
$ mv -i file.txt /path/to/destination/
To forcefully overwrite without being prompted, use the -f (force) option:
$ mv -f file.txt /path/to/destination/
Linux Commands
Linux Basic Commands Tutorial page: