移动命令 mv

一、命令详解


1.命令说明

mv命令是move的缩写,可以用来移动文件或者将文件改名(move (rename) files),是Linux系统下常用的命令,覆盖移动也是删除动作。

1、mv命令可以用来将源文件移至一个目标文件中,
2、将一组文件移至一个目标目录中。

源文件被移至目标有以下结果:

1、如果源文件和目标文件在同一个目录下,mv的作用就是改文件名


2、当目标文件是文件时,在不同目录下。目标文件与源文件同名,则源文件名(只能有一个)会变为此目标文件名,并覆盖己存在的同名文件。


3、当目标文件是目录文件时,如果目标文件是mv到另一目录文件的路径,源文件会被移到此目录下,且文件名不变。


mvcp的结果不同:
mv类似文件“剪切”,文件个数并未增加。
cp对文件进行复制,文件个数增加。

默认情况 mv有别命名 mv -i
‘ type mv mv is aliased to mv -i’
覆盖会有提示。但是加了-f覆盖就不会有提示。


2.语法格式

mv     [OPTION]...   SOURCE   ...   DIRECTORY
mv     [选项]  ...    源文件或目录 ... 目标文件或目录

3.选项描述

DESCRIPTION

      --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
  #force 强制的意思,如果目标文件已经存在,不会询问而直接覆盖
  #若目标文件或目录与现有的文件或目录重复,则直接覆盖现有的文件或目录且不会询问。
  -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
  #不覆盖已存在文件,如果您指定了-i、-f、-n 中的多个,仅最后一个生效。
  -S, --suffix=SUFFIX          override the usual backup suffix
  #与-b参数一并使用,可指定备份文件的所要附加的字尾,即替换备份文件后缀。
  -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
#设置目标的SELinux安全上下文 ,文件为默认类型                         

二、命令示例


案例1:同一目录下,相当于改名字

[root@centos7 app]#cat f2
word
1234
aaaaa
bbbbb
[root@centos7 app]#mv f2 f22
[root@centos7 app]#ls
cat.txt  f1           f22          f4  fifo_file  merrys   test.sh
dir1     f1_softlink  f2_hardlink  f5  ls.log     program  tomhome
[root@centos7 app]#cat f22
word
1234
aaaaa
bbbbb

案例2:不同目录,移动文件

[root@centos7 app]#ls ~
a  Desktop  Documents  f1           f{1-9}{a-b}  fcptest  Pictures  Templates
b  dir1     Downloads  f[1-9][a-b]  f333         Music    Public    Videos
[root@centos7 app]#ls
cat.txt  f1           f22          f4  fifo_file  merrys   test.sh
dir1     f1_softlink  f2_hardlink  f5  ls.log     program  tomhome
[root@centos7 app]#mv f22 /root/f22
[root@centos7 app]#ls ~
a        dir1       f1           f22      Music     Templates
b        Documents  f[1-9][a-b]  f333     Pictures  Videos
Desktop  Downloads  f{1-9}{a-b}  fcptest  Public

案例3:将多个文件移动到指定目录

[root@centos7 ~]#ls
a        dir1       f1           f22      Music     Templates
b        Documents  f[1-9][a-b]  f333     Pictures  Videos
Desktop  Downloads  f{1-9}{a-b}  fcptest  Public
[root@centos7 ~]#mv a b f22 /app
[root@centos7 ~]#ls /app
a  cat.txt  f1           f22          f4  fifo_file  merrys   test.sh
b  dir1     f1_softlink  f2_hardlink  f5  ls.log     program  tomhome
[root@centos7 ~]#ls
Desktop  Documents  f1           f{1-9}{a-b}  fcptest  Pictures  Templates
dir1     Downloads  f[1-9][a-b]  f333         Music    Public    Videos

案例4:将原文件移动到已存在的文件

默认情况 mv有别命名 mv -i
‘ type mv mv is aliased to mv -i’

[root@centos7 app]#cat f1
word
1234
aaaaa
bbbbb
[root@centos7 app]#cat /app/dir1/f1
i am empty
[root@centos7 app]#mv f1 /app/dir1/
mv: overwrite ‘/app/dir1/f1’? y
[root@centos7 app]#cat /app/dir1/f1
word
1234
aaaaa
bbbbb

如果目标已经存在同名文件,则询问是否覆盖。


案例5:目录的移动

注意:
-h给大小加单位,S是按文件大小排列,更容易方便阅读。

[root@centos7 app]#ls a
[root@centos7 app]#mv dir1 a
[root@centos7 app]#ls a
dir1

案例6:子目录里的文件移动到另一个子目录里

root@centos7 app]#ls dir1
[root@centos7 app]#mv /app/a/* /app/dir1/
[root@centos7 app]#ls dir1
dir1
[root@centos7 app]#ls /app/a

案例7:覆盖文件之前先行备份#mv -b

[[root@centos7 app]#touch f33
[root@centos7 app]#mv -b f22 f33
mv: overwrite ‘f33’? y
[root@centos7 app]#ls
a  cat.txt  f1           f2_hardlink  f33~  f5         ls.log  program  tomhome
b  dir1     f1_softlink  f33          f4    fifo_file  merrys  test.sh

案例8:强制执行 #mv -f

注意:
mv -f 是个危险的选项,使用的时候一定再三确认。
一般情况下不要加上-f

[root@centos7 app]#mv -f /app/dir1/f1 f1
[root@centos7 app]#cat f1
word
1234
aaaaa
bbbbb
[root@centos7 app]#cat /app/dir1/f1
cat: /app/dir1/f1: No such file or directory

案例9:移动文件 #mv -t

当需要移动多个源文件时,适用于这种方式,这时目标目录在前,源文件在后。

[root@centos7 a]#ls
[root@centos7 a]#cd /app
[root@centos7 app]#mv -t /app/a/ f33 dir1 f5 ls.log
[root@centos7 app]#ls /app/a
dir1  f33  f5  ls.log
[root@centos7 a]#mv -t ../../app  f33 f5
[root@centos7 a]#ls
dir1  ls.log
[root@centos7 a]#ls /app
a  cat.txt  f1_softlink  f33   f4  fifo_file  program  tomhome
b  f1       f2_hardlink  f33~  f5  merrys     test.sh

  • 7
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值