L7.linux命令每日一练 -- 第二章 文件和目录操作命令 -- cp和mv命令

2.7 cp:复制文件或目录

2.7.1 命令详解

【命令星级】 ★★★★★

【功能说明】

​ cp命令可以理解为英文单词copy的缩写,其功能为复制文件或目录。

【语法格式】

cp [option] [source] [dest]
cp [选项] [源文件] [目标文件]

​ **说明:**cp命令以及后面的选项和文件,每个元素之间都至少要有一个空格。

【选项说明】

​ 表2-9针对cp命令的参数选项进行了说明。

​ 表2-9 cp命令的参数选项及说明

在这里插入图片描述

2.7.2 使用范例

2.7.2.1 基础范例

​ **范例2-36:**无参数和带参数-a的比较。

[root@centos7 ~/test]#  pwd
/root/test
[root@centos7 ~/test]# ll -h	#查看当前文件的时间属性,读者以自己的系统时间为准。
total 0
drwxr-xr-x. 3 root root 18 Oct  6 17:26 dir1
drwxr-xr-x. 2 root root  6 Oct  6 16:42 dir2
drwxr-xr-x. 2 root root  6 Oct  6 16:42 dir3
-rw-r--r--. 1 root root  0 Oct  6 16:42 file1.txt
-rw-r--r--. 1 root root  0 Oct  6 16:42 file2.txt
-rw-r--r--. 1 root root  0 Oct  6 16:42 file3.txt
[root@centos7 ~/test]# cp file1.txt file4.txt	#复制file1.txt为file4.txt
[root@centos7 ~/test]# cp -a file1.txt file5.txt	#使用-a参数复制file1.txt为file5.txt。
[root@centos7 ~/test]# ll -h	#再次查看当前文件的时间属性。
total 0
drwxr-xr-x. 3 root root 18 Oct  6 17:26 dir1
drwxr-xr-x. 2 root root  6 Oct  6 16:42 dir2
drwxr-xr-x. 2 root root  6 Oct  6 16:42 dir3
-rw-r--r--. 1 root root  0 Oct  6 16:42 file1.txt
#此行是源文件。
-rw-r--r--. 1 root root  0 Oct  6 16:42 file2.txt
-rw-r--r--. 1 root root  0 Oct  6 16:42 file3.txt
-rw-r--r--. 1 root root  0 Oct  6 17:57 file4.txt
#此行是未使用任何参数。
-rw-r--r--. 1 root root  0 Oct  6 16:42 file5.txt
#此行是使用-a参数进行复制,属性不变。

​ 可以发现,使用-a参数进行复制时,文件的时间属性没有改变,-a参数的功能包含-p参数保持文件的属性功能。

扫描二维码关注公众号,回复: 14319368 查看本文章

​ **范例2-37:**使用-i参数的例子

[root@centos7 ~/test]# cp -i file1.txt file5.txt	#使用-i参数复制文件,会提示是否覆盖文件。
cp: overwrite ‘file5.txt’? ^C
[root@centos7 ~/test]# cp file1.txt file5.txt	#不适用-i参数结果是一样的,为什么呢?
cp: overwrite ‘file5.txt’? ^C
[root@centos7 ~/test]# alias cp	#原因是系统为cp命令默认设置了别名。
alias cp='cp -i'

​ CentOS系统默认为cp命令设置了别名,即增加了-i的参数。但是在Shell脚本中执行cp命令时,没有-i参数时并不会询问是否覆盖。这是因为命令行和Shell脚本执行时的环境变量不同。不过在脚本中一般使用命令的全路径。

​ **范例2-38:**使用-r参数复制目录。

[root@centos7 ~/test]# cp dir1 dir2/	#复制dir1到dir2,但结果显示跳过目录dir1。
cp: omitting directory ‘dir1’
[root@centos7 ~/test]# cp -r dir1 dir2/	#若使用-r参数则复制成功。
[root@centos7 ~/test]# tree dir2	#查看复制结果。
dir2
└── dir1
    └── sub1
        └── test

3 directories, 0 files
#提示:使用-a参数也可以达到相同的效果,因为-a参数相当于“dpr”三个参数。

2.7.2.2 技巧性范例

​ **范例2-39:**cp覆盖文件不提示是否覆盖的几种方法.。

​ **解题思路:**屏蔽系统默认的对应的命令别名。

​ 第一种方法,使用命令全路径,示例代码如下:

[root@centos7 ~/test]# cp file1.txt file2.txt
cp: overwrite ‘file2.txt’? y	#提示要输入y以确认覆盖,输入n则为不覆盖。
[root@centos7 ~/test]# which cp	#查看cp的系统别名。
alias cp='cp -i'
	/usr/bin/cp	#CentOS 7系统里是/usr/bin/co,为了兼容CentOS 6,将/bin指向了/usr/bin/cp。
[root@oldboyedu2 ~/test]#/bin/cp file1.txt file2.txt
#使用cp命令的绝对路径,即可屏蔽别名。

​ 第二中方法,命令开头使用反斜线\,示例代码如下:

[root@centos7 ~/test]# \cp file1.txt file2.txt
#使用“\”屏蔽系统别名。

​ 第三种方法,取消cp的别名,但重启系统或退出Xshell失效,示例代码如下:

[root@centos7 ~/test]# unalias cp	#alias设置别名,unalias取消别名,具体用法见对应章节的内容详解。
[root@centos7 ~/test]# cp file1.txt file2.txt

​ 第四种为杀鸡取卵的方法,用于开拓思路,不建议采用,示例代码如下:

[root@centos7 ~/test]# cat ~/.bashrc
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'	#编辑这个文件,注释掉本行,重启后生效。“.bashrc”这个文件在开机时会加载。
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi

​ **范例2-40:**快速备份文件案例。

​ 通常我们备份文件时会使用下面的格式:

[root@centos7 ~/test]# cp /etc/ssh/sshd_config  /etc/ssh/sshd_config.ori
#观察上面的格式,我们可以发现有一部分路径重复了。因此有了下面的快捷写法,使用“{}”的用法,两种方法是等效的。
[root@centos7 ~/test]# cp /etc/ssh/sshd_config{,.ori}
#这种方法的原理是bash对大括号的展开操作,/etc/ssh/sshd_config{,.ori}展开成/etc/ssh/sshd_config  /etc/ssh/sshd_config.ori再传给cp命令。

2.8 mv:移动或重命名文件、目录

2.8.1 命令详解

【命令星级】 ★★★★★

【功能说明】

​ mv命令可以理解为英文单词move的缩写,其功能是移动或重命名文件(move/rename files)。

【语法格式】

mv [option] [source] [dest]
mv [选项] [源文件] [目标文件]

​ **说明:**mv命令以及后面的选项和文件,每个元素之间都至少要有一个空格。

【选项说明】

​ 表2-10针对mv命令的参数选项进行了说明。

​ 表2-10 mv命令的参数选项及说明

在这里插入图片描述

2.8.2 使用范例

​ **范例2-41:**对文件改名的例子。

[root@centos7 ~/test]# ls
dir1  dir2  dir3  file1.txt  file2.txt  file3.txt  file4.txt  file5.txt
[root@centos7 ~/test]# mv file5.txt file6.txt 	#若file6.txt不存在,则将file5.txt重命名为file6.txt.
[root@centos7 ~/test]# ls
dir1  dir2  dir3  file1.txt  file2.txt  file3.txt  file4.txt  file6.txt
[root@centos7 ~/test]# mv file4.txt file6.txt #若file6.txt存在,则将file4.txt覆盖file6.txt
mv: overwrite ‘file6.txt’? y	#由于系统默认为mv设置了别名,因此会提示是否覆盖。
[root@centos7 ~/test]# alias mv
alias mv='mv -i'	#-i参数的功能是若目标文件存在,则会询问是否覆盖。
[root@centos7 ~/test]# ls
dir1  dir2  dir3  file1.txt  file2.txt  file3.txt  file6.txt
[root@centos7 ~/test]# \mv  file3.txt file6.txt #若使用“\”屏蔽系统别名,则不会询问是否覆盖了。
[root@oldboyedu2 ~/test]#ls
dir1  dir2  dir3  file1.txt  file2.txt  file6.txt

​ **范例2-42:**移动文件的例子。

​ 移动单个文件的示例代码如下:

[root@centos7 ~/test]# ls dir1
sub1
[root@centos7 ~/test]# mv file6.txt dir1/	#dir1为目录且该目录已存在,则移动file6.txt到dir1下,若dir1不存在,则重命名为dir1的普通文件。
[root@centos7 ~/test]# ls
dir1  dir2  dir3  file1.txt  file2.txt
[root@centos7 ~/test]# ls dir1
file6.txt  sub1

#移动多个文件:
[root@centos7 ~/test]# mv file1.txt file2.txt dir1/	#第一种方式:多个文件在前,目录在后。
[root@centos7 ~/test]# ls
dir1  dir2  dir3  
[root@centos7 ~/test]# ls dir1
file1.txt  file2.txt  file6.txt  sub1
[root@centos7 ~/test]# mv dir1/file* .	#还原实验环境,“file*”匹配所有以file开头的文件。
[root@centos7 ~/test]# ls
dir1  dir2  dir3  file1.txt  file2.txt  file6.txt
[root@centos7 ~/test]# ls dir1
sub1

​ **范例2-43:**将源和目标调换移动文件到目录(-t参数)。

[root@centos7 ~/test]# ls
dir1  dir2  dir3  file1.txt  file2.txt  file6.txt
[root@centos7 ~/test]# mv -t dir1/ file1.txt file2.txt file6.txt 	#使用-t参数将源和目标进行调换,-t后接目录,最后是要移动的文件。
[root@centos7 ~/test]# ls
dir1  dir2  dir3
[root@centos7 ~/test]# ls dir1/
file1.txt  file2.txt  file6.txt  sub1

​ **范例2-44:**移动目录的例子。

[root@centos7 ~/test]# ls 
dir1  dir2  dir3
[root@centos7 ~/test]# mv dir1 dir5	#目录dir5不存在,将目录dir1改名为dir5
[root@centos7 ~/test]# ls
dir2  dir3  dir5
[root@centos7 ~/test]# ls dir5/
file1.txt  file2.txt  file6.txt  sub1
[root@centos7 ~/test]# mv dir2 dir5	#若存在目录dir5,则将dir2移动到dir5中
[root@centos7 ~/test]# ls dir5/
dir2  file1.txt  file2.txt  file6.txt  sub1
[root@centos7 ~/test]# mv dir3/ dir5/	#若在源目录结尾加“/”则不影响结果,但是为了规范和简单起见,建议不加“/”。
[root@centos7 ~/test]# ls
dir5
[root@centos7 ~/test]# ls dir5/
dir2  dir3  file1.txt  file2.txt  file6.txt  sub1

2.8.3 关于mv命令的使用小结

​ 表2-11针对mv命令的使用进行了总结。

​ 表2-11 mv命令使用小结

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_25599925/article/details/125351282