linux一步一脚印---mv命令

1 命令功能(简要说明):

    命令可以用来移动文件或者更改文件名字,经常用来备份文件或者目录,mv --> move

2 命令语法:

    mv【选项】 【源文件或目录名】 【目标文件或目录名】    #注:【】中的内容为非必选项

3 命令选项(只做常用命令参数讲述):

  使用帮助命令:man mkdir 或 mkdir -help

 --backup[=CONTROL] 为每个已存在的目标文件创建备份
-b 类似--backup 但不接受参数
-f, --force 覆盖前不询问,如果目标文件已经存在,不会询问而直接覆盖。
-i, --interactive 当目标文件已经存在当前目录,覆盖前会进行询问。
-n, --no-clobber 不覆盖已存在文件   如果您指定了-i、-f、-n 中的多个,仅最后一个生效。
--strip-trailing-slashes 去掉每个源文件参数尾部的斜线
-S, --suffix=SUFFIX 替换常用的备份文件后缀
-t, --target-directory=DIRECTORY 将所有参数指定的源文件或目录移动至指定目录,既该功能适用于移动多个源文件到一个目录的情况,注意此时目标目录在前,各个源文件在后
-T, --no-target-directory 将目标文件视作普通文件处理
-u, --update 只在源文件文件比目标文件新,或目标文件不存在时才进行移动

4 使用范例:

  (1)mv 文件改名

[root@localhost command_test]# ls
av.log
[root@localhost command_test]# mv av.log av.mp4
[root@localhost command_test]# ls
av.mp4
[root@localhost command_test]# 

  (2)mv 【文件名或目录】 【文件名或目录】 ------移动文件

#首先创建目录dir1
[root@localhost command_test]# mkdir dir
[root@localhost command_test]# ll
总用量 4
-rw-r--r--. 1 root root    0 7月  26 23:27 av.mp4
drwxr-xr-x. 2 root root 4096 7月  26 23:35 dir1
[root@localhost command_test]# 

#创建测试文件canglaoshi.txt,并移动文件到dir1目录下
[root@localhost command_test]# touch canglaoshi.txt
[root@localhost command_test]# ls
av.mp4  canglaoshi.txt  dir1
[root@localhost command_test]# mv canglaoshi.txt  dir1
[root@localhost command_test]# ls
av.mp4  dir1
[root@localhost command_test]# 

#创建canglaoshi.txt,因为canglaoshi.txt文件在上一步骤已经存在dir1目录下,继续将文件移动到dir1时相当于覆盖操作,覆盖前会进行询问
[root@localhost command_test]# touch canglaoshi.txt
[root@localhost command_test]# ls
av.mp4  canglaoshi.txt  dir1
[root@localhost command_test]# mv canglaoshi.txt  dir1
mv:是否覆盖"dir1/canglaoshi.txt"? y
[root@localhost command_test]# ls
av.mp4  dir1
[root@localhost command_test]# 

#再次创建canglaoshi.txt,这次试用-f 参数强制移动文件,无视目标目录是否已经存在目录,覆盖不会询问
[root@localhost command_test]# touch canglaoshi.txt
[root@localhost command_test]# ls
av.mp4  canglaoshi.txt  dir1
[root@localhost command_test]# mv -f canglaoshi.txt  dir1
[root@localhost command_test]# ls
av.mp4  dir1
[root@localhost command_test]#

 

  (3)mv 【文件1】 【文件2】 【文件3】 【目标目录】  ------- 多个文件移动到同一目录,注意-t参数的区别影响

#首先我们先创建文件和目录dir1
[root@localhost command_test]# touche canglaoshi.txt xioacang.txt boduo.txt
-bash: touche: command not found
[root@localhost command_test]# touch canglaoshi.txt xioacang.txt boduo.txt 
[root@localhost command_test]# ls
boduo.txt  canglaoshi.txt  xioacang.txt
[root@localhost command_test]# mkdir dir1
[root@localhost command_test]# ls
boduo.txt  canglaoshi.txt  dir1  xioacang.txt
#注意mv命令在移动多个文件时,最后一个参数为已存在的目标目录,否则则会出错
[root@localhost command_test]# mv boduo.txt canglaoshi.txt xioacang.txt dir1
[root@localhost command_test]# ls
dir1
[root@localhost command_test]# cd dir1
[root@localhost dir1]# ls
boduo.txt  canglaoshi.txt  xioacang.txt
[root@localhost dir1]# 


#下列示例展示同为多个文件移动,但有一点不用的是,mv命令使用-t 参数,则需把目标目录放在第一为,后接需要移动的多个文件
[root@localhost command_test]# pwd
/home/command_test
[root@localhost command_test]# ls
dir1
[root@localhost command_test]# cd dir1
[root@localhost dir1]# ls
boduo.txt  canglaoshi.txt  xioacang.txt
[root@localhost dir1]# mv -t /home/command_test boduo.txt canglaoshi.txt xioacang.txt 
[root@localhost dir1]# ls
[root@localhost dir1]# 
[root@localhost dir1]# cd ..
[root@localhost command_test]# ls
boduo.txt  canglaoshi.txt  dir1  xioacang.txt
[root@localhost command_test]# 

  (4)mv  目录的移动

#创建两个二级目录,并把目录dir1以及其子目录移动到dir3,整个目录移动很方便
[root@localhost command_test]# mkdir -p dir1/dir2
[root@localhost command_test]# mkdir -p dir3/dir4  
[root@localhost command_test]# ls
dir1  dir3
[root@localhost command_test]# mv dir1 dir3
[root@localhost command_test]# ls
dir3
[root@localhost command_test]# cd dir3
[root@localhost dir3]# ls
dir1  dir4
[root@localhost dir3]# cd dir1
[root@localhost dir1]# ls
dir2
[root@localhost dir1]# 

  (5)mv 使用相对路径移动目录到上一级目录

#使用相对路径移动当前目录下所有东西到上一级目录
[root@localhost command_test]# mkdir dir1
[root@localhost command_test]# ls
dir1
[root@localhost command_test]# cd dir1
[root@localhost dir1]# touch canglaoshi.txt xiaocang.txt
[root@localhost dir1]# ls
canglaoshi.txt  xiaocang.txt
[root@localhost dir1]# mv * ../
[root@localhost dir1]# ls
[root@localhost dir1]# cd ..
[root@localhost command_test]# ls
canglaoshi.txt  dir1  xiaocang.txt
[root@localhost command_test]# 

 

  

原文地址:https://www.cnblogs.com/king-of-purple/p/9371481.html