Common Linux commands (6): mv command (move files/directories)

1. Introduction to commands

mv (move) is used to move or rename files and directories.

If the target file already exists when moving a file, the contents of the target file will be overwritten.

The mv command can be used to move source files to a destination file, or move a group of files to a destination directory. There are two different results when the source file is moved to the target file:

(1) If the target file is a directory, the original file will be moved to this directory and the file name will remain unchanged. When the target file is a directory, there can be multiple source files or directory parameters, and all source files will be moved to the target directory. All files moved to this directory will retain their previous filenames.

(2) If the target file is not a directory, the original file name (there can only be one) will be changed to the target file name, and the existing file with the same name will be overwritten. If the original file and the target file are in the same directory, the function of mv is to modify the file name.

Notice:

(1) The results of mv and cp are different. mv is like "moving" files, and the number of files does not increase. While cp copies files, the number of files increases.

(2) Although its manual does not specify this, using mv to move a directory will fail if the target directory is not empty. If it is safe, you can use the cp command instead.

cp -rf yourdir dstdir && rm -r yourdir

2. Command format

mv [options] source dest
mv [options] source... directory

Option description

--backup=[=CONTROL]
	若需覆盖文件,则覆盖前先行备份
-b
	当文件存在时,覆盖前,为其创建一个备份。功能类似于--backup,但是不需要接收参数
-f, --force
	若目标文件或目录已存在,则直接覆盖不进行提示。为缺省行为。
-i, --interactive
	交互式操作,若目标文件或目录已存在,则询问用户是否覆盖。输入y表示同意;输入n表示不同意
-n, --no-clobber
	不覆盖现有的同名文件或目录;如果同时指定多个以下选项,-i, -f, -n,则只有最后一个生效
--strip-trailing-slashes
	删除参数 SOURCE 中所有目录末端的斜杠
-S, --suffix=SUFFIX
	为备份文件指定后缀,而不使用默认的后缀
-t, --target-directory=DIRECTORY
	指定源文件要移动到的目标目录
-T, --no-target-directory
	将 DEST 视为普通文件
-u, --update
	当源文件比目标文件新或者目标文件不存在时,才执行移动操作
-v, --verbose
	冗余模式执行 mv,解释 mv 命令的执行过程

Commonly used parameters:

  • -i: If a file with the same name already exists in the specified directory, first ask whether to overwrite the old file;
  • -f: does not give any instructions when the mv operation wants to overwrite an existing target file;

mv parameter settings and running results

Command format operation result
mv filename filename Change the source file name to the target file name
mv file name directory name Move files to target directory
mv directory name directory name If the target directory already exists, move the source directory to the target directory; if the target directory does not exist, rename it.
mv directory name file name Error

3. Common examples

(1) Rename the file aaa to bbb.

mv aaa bbb

(2) Move + rename

mv  /home/test/a.txt   /home/demo/c.txt

(3) Move all files in the directory /home/test to the current directory (indicated by .).

mv /home/test/*  .

(4) Place the info directory into the logs directory. Note that if the logs directory does not exist, this command will rename info to logs.

mv info/ logs 

(5) Interactive operation, if the target file or directory already exists, ask the user whether to overwrite it.

mv -i test1 test2

Guess you like

Origin blog.csdn.net/weixin_49114503/article/details/132993612