2.11.CP命令
cp(copy简写)命令:用来将一个或多个源文件或者目录复制到指定的目的文件或目录。
cp –r:复制目录
-i:安全选项
-------------------------------------------------------------------------------------------
cp命令:拷贝/etc/passwd/文件到/tmp/目录下从命名成01.txt
[[email protected] ~]# cp /etc/passwd /tmp/01.txt
[[email protected] ~]# ls /tmp
01.txt
daizhihong
daizhihong1
ks-script-9pf2WE
ls1
systemd-private-17411cf38aa84739852c7f37c5bb6b71-chronyd.service-qj4Br8
systemd-private-17411cf38aa84739852c7f37c5bb6b71-vgauthd.service-LrCTDS
systemd-private-17411cf38aa84739852c7f37c5bb6b71-vmtoolsd.service-TpTavE
systemd-private-600bc20c894f45978b1376aee644f335-chronyd.service-9IYyvb
systemd-private-600bc20c894f45978b1376aee644f335-vgauthd.service-vbgiVB
systemd-private-600bc20c894f45978b1376aee644f335-vmtoolsd.service-mLvpl3
yum.log
------------------------------------------------------------------------------------------------
cp -r拷贝目录:新建一个目录/tmp/daizhihong3/,然后把/tmp/daizhihong/目录拷贝到/tmp/daizhihong3/目录内
[[email protected] ~]# mkdir -pv /tmp/daizhihong3/
mkdir: 已创建目录 "/tmp/daizhihong3/"
[[email protected] ~]# cp -r /tmp/daizhihong/ /tmp/daizhihong3/
[[email protected] ~]# tree /tmp/daizhihong3/
/tmp/daizhihong3/
└── daizhihong
└── 11.txt
1 directory, 1 file
注:有时候在拷贝目录当中拷贝源有加“/”,而目标没有加“/”,在Linux系统当中拷贝目录的时候是需要带“/”的。
------------------------------------------------------------------------------------------------
!$ :表示执行上一条命令的最后一个参数
[[email protected] ~]# tree /tmp/daizhihong3/
/tmp/daizhihong3/
└── daizhihong
└── 11.txt
1 directory, 1 file
[[email protected] ~]# tree !$(执行tree命令上一条的最后一个参数)
tree /tmp/daizhihong3/
/tmp/daizhihong3/
└── daizhihong
└── 11.txt
1 directory, 1 file
--------------------------------------------------------------------------------------------
“-i”的参数:在我们使用which查询命令绝对路径的时候会出现一个“-i”的参数,这个参数的意义是一个安全参数,询问是否执行就是因为加了“-i”的参数
[[email protected] ~]# which cp
alias cp='cp -i'
/usr/bin/cp
[[email protected] ~]#
[[email protected] ~]# which rm
alias rm='rm -i'
/usr/bin/rm
[[email protected] ~]# cp /tmp/daizhihong/11.txt /tmp/daizhihong1/
cp:是否覆盖"/tmp/daizhihong1/11.txt"? n(加了“-i”参数就会提示是否需要覆盖或者执行rm命令的时候是否需要删除,所以“-i”是一个安全参数)
键入命令的时候不询问命令格式如下:
[[email protected]~]# /usr/bin/cp/tmp/daizhihong/11.txt /tmp/daizhihong1/
[[email protected] ~]#
绝对路径命令格式执行就不会提示询问
-------------------------------------------------------------------------------------------
以下这个实验说明:当目标目录已经存在的时候会把源目录直接放到目标目录下,如果目标目录不存在它会把源目录拷贝过来并且改名字
[[email protected] ~]# tree /tmp/daizhihong
/tmp/daizhihong
└── 11.txt
0 directories, 1 file
[[email protected] ~]# tree /tmp/daizhihong1
/tmp/daizhihong1
└── 01
└── 11.txt
1 directory, 1 file
[[email protected] ~]# cp -r /tmp/daizhihong/ /tmp/daizhihong1/
[[email protected] ~]# ls /tmp/daizhihong1/
01 daizhihong
[[email protected] ~]# tree /tmp/daizhihong1
/tmp/daizhihong1
├── 01
│ └── 11.txt
└── daizhihong(当目标目录已经存在的时候会把源目录直接放到目标目录下)
└── 11.txt
2 directories, 2 files
[[email protected] ~]#
原文地址:http://blog.51cto.com/13577820/2066206