Linux provides the mv command in order to move files and directories. The mv command can be also used for Unix, BSD even MacOSX operating systems in the same way. The mv command makes cuts to the specified files and directories and pastes them into the specified path. The move operation also called rename files and folders. The specified files and directories can be renamed in the same directory or by moving them to different paths.
mv Command Syntax
The mv command has the following syntax.
mv OPTION SOURCE DESTINATION
- OPTION is the option about mv command. This is optional.
- SOURCE is the source files or directories. This is required.
- DESTINATION is the destination files or directories. This is required.
Print Help Information
The mv command help information can be displayed with the –help option. This help information provide options about the mv command.
mv --help
Move File
Single file can be easily moved by specifiying its name. In the following example we move the file named “file.txt” into the directory named data1.
mv file.txt data1/
While moving files absolute or full path of the destination can be specified like below. In the following example, we move the file named “file.txt” into the “/home/ismail/data1“.
mv file.txt /home/ismail/data1/
Alternatively we can use other path specifiers like parent directory etc.
mv file.txt ../../data1
Rename File
In previous example we have moved a single file into different destinations with the same name. We can also use the mv command in order to change file name in the same directory or moving different directories.
mv file.txt myfile.txt
We can rename file by moving into different directories like below. We will add the new name of the file to the destination path or directory information.
mv file.txt /home/ismail/data1/myfile.txt
Move Multiple Files
The mv command can be used to move multipe files in a single execution. The multiple files are specified after the mv command. The destination directory or path is specified as the last parameter. In the following example we move the files named “file1”, “file2”, “file3” into the directory named data2.
mv file1 file2 file3 data2
Also we can specify the absolute path to move files.
mv file1 file2 file3 /home/ismail/data/
Move Files According To Type or Extension
The mv command supports the glob operator. The glob operator can be used to specify files acording to their extensions. For example text files can be specified with the *.txt . In the following example we move all text files into the directory named data2.
mv *.txt data2
Alternatively we can specify the absolute or full path of the remote directory.
mv *.txt /home/ismail/data
Move Directory
The mv command can be also used to move directory. The directory is specified like a file and the destination path or directory specified too. In the following example we move the directory named data4 into the directory named data5.
mv data4 data5/
Alternatively we can specify the absolute or full path of the remote directory.
mv data4 /mnt/data5/
Prompt Before Overwrite
During the move, the destination directory may contain a file or directory with the same name. In this case, the mv command overwrites by default without warning or asking the user. This is generally a problem and causes data loss. We can enable prompting before overwrite by using the -i option.
mv -i file.txt data/
The following question is prompted or asked. Just type and enter “y” to over write existing file to overwrite. If you do not overwrite into the destination just type “n” and press enter.
mv: overwrite 'file.txt'? n
Prevent Overwrite
By default, the mv command overwrites into the existing destination file or directories. This can be prevented by using the -n option.
mv -n file.txt data/
Move Only Newer Files
Another useful option and feature of the mv command is the ability or move only newer files and skip old or already existing and same files. The -u or –update option can be used to move only newer files.
mv -u files/ myfiles/
Verbose
By default, the move operations are not displayed explicitly. But if we want we can display verbose information about the move by using the -v option.
mv -v files/ myfiles/