linux 命令详解

本文主要内容来自Linux man 手册

命令名称:

mv(move)移动/重命名文件

命令用法:

mv [选项]... [-T] 源文件 目标文件
mv [选项]... 源文件... 目录
mv [选项]... -t 目录 源文件...

命令概述:

将源文件重命名为目标文件,或者将一个或多个源文件移动到目标目录。

命令参数:

--backup[=CONTROL]
   	备份目标文件,备份文件后缀默认为'~',除非设置了--suffix(见后文)或者 
   	SIMPLE_BACKUP_SUFFIX。版本控制可以由 --backup 和 VERSION_CONTROL 
   	环境变量决定。
   	CONTROL 或 VERSION_CONTROL可以为以下几种:
   	none 或 off ,不使用备份,即使使用了 --backup 参数
   	numbered 或 t,用数字表示备份版本
   	existing 或 nil,如果数字备份已存在,继续使用数字备份
   	simple 或 never,一直使用简单的备份(多次备份采用覆盖的方式)
	
-b
	和 --backup 类似,但是不带参数(简单备份)
	  
-f,--force
	在覆盖前不提示(询问)
		
-i,--interactive
	 在覆盖前进行提示(询问)

-n,  --no-clobber 无冲突
	不覆盖已存在的文件,如果同时指定了-i,-f,-n三个参数,只有最后一个(使用时的顺序)生效。
	   
--strip-trailing-slashes 
	删除每个源文件后面的斜杠

-S, --suffix=SUFFIX
	修改常用的备份后缀,备份文件后缀默认为'~',除非设置了--suffix 或者 
	SIMPLE_BACKUP_SUFFIX。

-t, --target-directory=DIRECTORY
	将所有源文件的参数复制到目录中【mv的三大用法之一】

-T, --no-target-directory
	将目标文件当成普通文件,而不考虑是否为目录(不知有啥用)

-u, --update
	仅当源文件比目标文件新或者目标文件缺失时才移动

-v, --verbose 冗长
	说明操作过程
	   
-Z  
	将安全增强型 linux 安全上下文设置为默认类型(???)

--help 
	显示帮助信息

--version 
	显示版本信息

示例:

1. mv 源文件 目标文件

不添加任何参数选项,且源文件只有一个,为mv的第一种用法,简单的文件移动或改名。

xiaohui@ubuntu:~/work/mv_learn$ mv a.c dir/a.c
xiaohui@ubuntu:~/work/mv_learn$ mv dir/a.c ./rename.c
xiaohui@ubuntu:~/work/mv_learn$ ls rename.c a.c
ls: cannot access 'a.c': No such file or directory
rename.c
xiaohui@ubuntu:~/work/mv_learn$ 

如果目标文件名字与源文件相同(但不能同路径),则只是将源文件位置改变;如果目标文件名字和源文件不同,则实现改名,改名时源文件和目标文件的父目录可以相同。

2. mv 源文件… 目录

不添加任何选项参数,源文件可以不止一个,目标文件必须为目录文件,这是mv的第二种用法,也就是批量移动。

xiaohui@ubuntu:~/work/mv_learn$ mv b.c c.c dir/
xiaohui@ubuntu:~/work/mv_learn$ mv dir/ dir_tmp/
xiaohui@ubuntu:~/work/mv_learn$ ls dir_tmp/ -lR
dir_tmp/:
total 4
drwxrwxr-x 2 xiaohui xiaohui 4096 7月  18 20:41 dir
-rw-rw-r-- 1 xiaohui xiaohui    0 7月  18 20:41 tmp1.c
-rw-rw-r-- 1 xiaohui xiaohui    0 7月  18 20:41 tmp2.c

dir_tmp/dir:
total 0
-rw-rw-r-- 1 xiaohui xiaohui 0 7月  18 20:36 b.c
-rw-rw-r-- 1 xiaohui xiaohui 0 7月  18 20:24 c.c
xiaohui@ubuntu:~/work/mv_learn$ 

和cp不同的是,文件移动时可以移动整个目录及其内容,即自带递归功能。

3. mv -t 目录 源文件…

不添加任何选项参数(-t 参数比较特殊,是mv命令的第三种用法)第三种用法和第二种类似,只是有了-t参数,目录的位置可以随意改动,甚至可以放到多个源文件中间(但必须在-t后面)。

xiaohui@ubuntu:~/work/mv_learn$ mv dir/ b.c c.c -t
mv: option requires an argument -- 't'
Try 'mv --help' for more information.
xiaohui@ubuntu:~/work/mv_learn$ mv -t dir/ b.c c.c 
xiaohui@ubuntu:~/work/mv_learn$ 

“-t 目录”等同于“–target-directory=DIRECTORY”
“长选项的强制参数对于短选项也是强制的”,这句话指的就是像-t的这类选项:–target-deirectory这个长选项后面必须接参数,所以-t这个短选项也必须带参数(即目标目录),但不是用“=”相连,而是用空格。

4. mv --backup[=CONTROL] 源文件/… 目标文件/目录

如果CONTRL为simple或never时,使用简单的备份,即只有一个版本的备份。(备份方法:先将要备份的文件内容复制到一个中间文件中,然后再使用cp命令,将中间文件作为cp命令的目标文件,选择备份的选项,源文件可以任意指定(备份完成后中间文件的内容将变成源文件内容),复制完成后会生成一个和中间文件同名的备份文件)。

上面的内容可能有些难以理解(可能我的认识不全正确)。

:将cp换成mv进行备份,源文件会消失,感觉不实用。只不过cp备份时,如果不添加保留属性的选项(如-a),目标文件的属性和源文件不全相同,但备份文件(带~后缀)的属性和备份前的目标文件是相同的。
备份相关的内容可以参考cp命令:Linux命令详解之 cp

5. mv -f 源文件/… 目标文件/目录

同–force,如果目标文件/目录已存在,则直接覆盖(这是默认情况)。

6. mv-i 源文件/… 目标文件/目录

同–interactive,文件已存在会询问用户是否覆盖,回Y/y则覆盖。

xiaohui@ubuntu:~/work/mv_learn$ mv c.c dir/ -i
mv: overwrite 'dir/c.c'? n
xiaohui@ubuntu:~/work/mv_learn$ mv c.c dir/ -i
mv: overwrite 'dir/c.c'?  
xiaohui@ubuntu:~/work/mv_learn$ mv c.c dir/ -i
mv: overwrite 'dir/c.c'? y
xiaohui@ubuntu:~/work/mv_learn$ ls c.c
ls: cannot access 'c.c': No such file or directory
xiaohui@ubuntu:~/work/mv_learn$ 

7. mv -n 源文件/… 目标文件/目录

同–no-clobber,不覆盖已存在的文件。-i,-f和-n三个参数优先级相同,同时用的时候,放最后的生效。

xiaohui@ubuntu:~/work/mv_learn$ mv c.c dir/c.c -i -fn
xiaohui@ubuntu:~/work/mv_learn$ ls c.c
c.c
xiaohui@ubuntu:~/work/mv_learn$ mv c.c dir/c.c -f -n -i
mv: overwrite 'dir/c.c'? n
xiaohui@ubuntu:~/work/mv_learn$ 

man手册

以下为 mv 命令手册原文:

MV(1)                            User Commands                           MV(1)

NAME
       mv - move (rename) files

SYNOPSIS
       mv [OPTION]... [-T] SOURCE DEST
       mv [OPTION]... SOURCE... DIRECTORY
       mv [OPTION]... -t DIRECTORY SOURCE...

DESCRIPTION
       Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

       Mandatory  arguments  to  long  options are mandatory for short options
       too.

       --backup[=CONTROL]
              make a backup of each existing destination file

       -b     like --backup but does not accept an argument

       -f, --force
              do not prompt before overwriting

       -i, --interactive
              prompt before overwrite

       -n, --no-clobber
              do not overwrite an existing file

       If you specify more than one of -i, -f, -n, only the  final  one  takes
       effect.

       --strip-trailing-slashes
              remove any trailing slashes from each SOURCE argument

       -S, --suffix=SUFFIX
              override the usual backup suffix

       -t, --target-directory=DIRECTORY
              move all SOURCE arguments into DIRECTORY

       -T, --no-target-directory
              treat DEST as a normal file
              
       -u, --update
              move  only  when  the  SOURCE file is newer than the destination
              file or when the destination file is missing

       -v, --verbose
              explain what is being done

       -Z, --context
              set SELinux security context of destination file to default type

       --help display this help and exit

       --version
              output version information and exit

       The  backup  suffix  is  '~',  unless  set  with   --suffix   or   SIM‐
       PLE_BACKUP_SUFFIX.   The version control method may be selected via the
       --backup option or through the  VERSION_CONTROL  environment  variable.
       Here are the values:

       none, off
              never make backups (even if --backup is given)

       numbered, t
              make numbered backups

       existing, nil
              numbered if numbered backups exist, simple otherwise

       simple, never
              always make simple backups

AUTHOR
       Written by Mike Parker, David MacKenzie, and Jim Meyering.

REPORTING BUGS
       GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
       Report mv translation bugs to <http://translationproject.org/team/>

COPYRIGHT
       Copyright  ©  2016  Free Software Foundation, Inc.  License GPLv3+: GNU
       GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
       This is free software: you are free  to  change  and  redistribute  it.
       There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
       rename(2)

       Full documentation at: <http://www.gnu.org/software/coreutils/mv>
       or available locally via: info '(coreutils) mv invocation'

GNU coreutils 8.25               February 2017                           MV(1)
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐