The MV (move) command in Linux moves files and directories. Frequently used to back up files and directories
1. Command format:
MV [option] source file or directory destination file or directory
Grammar:
MV [OPTION] ... [-T] SOURCE DEST
MV [OPTION] ... SOURCE ... DIRECTORY
MV [OPTION] ...-t DIRECTORY SOURCE ...
2. Command function:
Depending on the second parameter type in the MV command (either the destination file or the destination directory), the MV command renames the file or moves it to a new directory. When the second parameter type is a file, the MV command completes the file rename, at which point the source file can only have one (or the source directory name), which renames the given source file or directory to the specified destination file name. When the second parameter is a directory name that already exists, the source file or directory parameter can have more than one, and the MV command moves the source files specified by each parameter to the destination directory. When you move a file across a file system, the MV copies the original file, and the link to the file is lost.
3. Command parameters:
-f:force mandatory Meaning, if the target file already exists, will not ask and directly overwrite
-I: If the target file (destination) already exists, you will be asked to overwrite
-U: If the target file already exists and source is newer, update (update)
-B: Overwrite pre-backup if file is required
-T: Specifies the target directory for MV, which is suitable for moving multiple source files to a directory, where the target directory is in front of the source file
command example:
1. Copy a folder, create a directory, and move the files to the directory
[[email protected] ~]# Cd/tmp[[email protected] tmp]# CP ~/.BASHRC bashrc[[email protected] tmp]# mkdir Mvtest[[email Pro Tected] tmp]# mv BASCRC mvtest
2. Directory renaming
[[Email protected] tmp]# mv mvtest mvtest2[[email protected] tmp]# lsbitrock_installer.log Dejavusans.ttf mvtest2 tmpwx Kbqk Yum.log
3. Move multiple Files
[[email protected] tmp]# CP ~/.BASHRC Bashrc1[[email protected] tmp]# CP ~/.BASHRC bashrc2[[email protected] tmp]# MV Bash RC1 Bashrc2 Mvtest2[[email protected] tmp]# LS/TMP/MVTEST2/BASHRC bashrc1 bashrc2
Note: If you have more than one source file or directory, the last destination file must be "directory"
The 4.-i parameter, renaming the file, if the target already exists, asks whether to overwrite
[Email protected] mvtest2]# mv-i bashrc1 bashrc2mv:overwrite ' bashrc2 '? Y
5.-f parameter, forced renaming, even if the target name exists, is also directly overwritten
[Email protected] mvtest2]# mv-f bashrc1 bashrc2
6. Move all files under the current folder to the top level directory
[[email protected] mvtest2]# ls mvtest2 has a bashrc2 file Bashrc2[[email protected] mvtest2]# MV *. /move files under Mvtest2 to the previous level directory [[email protected] mvtest2]# CD-Fallback to previous level directory/tmp[[email protected] tmp]# LS View BASHRC2 has moved to this directory bashrc2 bitrock_installer.log dejavusans.ttf mvtest2 tmpwxkbqk yum.log[[em AIL protected] tmp]#
7. Move the files in the directory to another sub-directory
[[email protected] mvtest2]# ls There are 4 txt files under mvtest2 1.txt 2.txt 3.txt 4.txt[[email protected] mvtest2]# cd - back to top level directory/tmp[[email protected] tmp] # mkdir mvtest3 Create Mvtest3 folder [ [email protected] tmp]# ls bashrc2 dejavusans.ttf mvtest3 yum.logbitrock_installer.log mvtest2 tmpwxkbqk[[email protected] tmp]# mv mvtest2/*.txt mvtest3 Move the TXT file under Mvtest2 to Mvtest3 [[email protected] tmp]# ls mvtest3 see if the files under Mvtest3 are copied over 1.txt 2.txt 3.txt 4.txt[[email protected] tmp]# ls mvtest2 There are no more than 4 txt files under Mvtest2 [[email protected] tmp]#
8.-b parameter, the file is overwritten before making a simple backup
[[email protected] mvtest3]# ls2.txt 3.txt 4.txt 888.txt[[email protected] mvtest3]# mv 2.txt-b 888.txt-b parameter Number Mv:overwrite ' 888.txt '? Y[[email protected] mvtest3]# lltotal 0-rw-r--r--. 1 root root 0 Apr 09:37 3.txt-rw-r--r--. 1 root root 0 Apr 09:37 4.txt-rw-r--r--. 1 root root 0 Apr 09:37 888.txt-rw-r--r--. 1 root root 0 Apr 09:37 888.txt~ ~ for backup files
Description
-B does not accept parameters, and MV will read the environment variable Version_control as a backup strategy.
--backup This option specifies a total of four backup strategies if the target file is present:
1.control=none or off: not backed up.
2.control=numbered or T: Digitally numbered backup
3.control=existing or nil: if there is a digitally numbered backup, continue numbering the backup M+1...N:
There is a digitally numbered file log2.txt.~1~ before performing the MV operation, then execution will produce log2.txt~2~, and so on. If you don't have a digitally numbered file before, use the simple backup described below.
4.control=simple or never: Use simple backup: A simple backup is made before being overwritten, a simple backup can only have one copy, and a simple backup will be overwritten when it is overwritten again.
Learning Reference:
Http://www.cnblogs.com/peida/archive/2012/10/27/2743022.html
This article is from the blog "Write to Yourself", so be sure to keep this source http://zhaodongwei.blog.51cto.com/4233742/1762844
Linux MV Command