CP命令:
用来将一个或多个源文件或者目录复制到指定的目的文件或目录。它能够将单个源文件复制成一个指定文件名的具体的文件或一个已经存在的目录下。cp命令还支持同时复制多个文件,当一次复制多个文件时,目标文件参数必须是一个已经存在的目录,不然将出现错误
参数:
-a:此参数的效果和同时指定"-dpR"参数相同;
-d:当复制符号链接时,把目标文件或目录也创建为符号链接,并指向与源文件或目录链接的原始文件或目录;
-f:强行复制文件或目录,不论目标文件或目录是否已存在;
-i:覆盖既有文件以前先询问用户;
-l:对源文件创建硬链接,而非复制文件;
-p:保留源文件或目录的属性;
-R/r:递归处理,将指定目录下的全部文件与子目录一并处理;
-s:对源文件创建符号链接,而非复制文件;
-u:使用这项参数后只会在源文件的更改时间较目标文件更新时或是名称相互对应的目标文件并不存在时,才复制文件;
-S:在备份文件时,用指定的后缀“SUFFIX”代替文件的默认后缀;
-b:覆盖已存在的文件目标前将目标文件备份;
-v:详细显示命令执行的操做。
实践操做:
[root@centos7-02 /]# ls /data/
test1.txt test2.txt test3.txt test4.txt
[root@centos7-02 /]# cp /data/test1.txt /tmp/test/
[root@centos7-02 /]# tree /tmp/test/
/tmp/test/
└── test1.txt
0 directories, 1 file
[root@centos7-02 /]# cp /data/test1.txt /tmp/test/
cp:是否覆盖"/tmp/test/test1.txt"? n
[root@centos7-02 /]# \cp /data/test1.txt /tmp/test/
[root@centos7-02 /]# cp /data/test1.txt /tmp/test/
cp:是否覆盖"/tmp/test/test1.txt"? n
[root@centos7-02 /]# which cp
alias cp='cp -i'
/usr/bin/cp
[root@centos7-02 /]# /usr/bin/cp /data/test1.txt /tmp/test/
[root@centos7-02 /]#
[root@centos7-02 /]# cp -r /data/ /tmp/test/
[root@centos7-02 /]# tree tmp/test/
tmp/test/
├── data
│ ├── test1.txt
│ ├── test2.txt
│ ├── test3.txt
│ └── test4.txt
└── test1.txt
1 directory, 5 files
[root@centos7-02 /]# cp -r /data/ /tmp/test/
cp:是否覆盖"/tmp/test/data/test1.txt"? n
cp:是否覆盖"/tmp/test/data/test2.txt"? n
cp:是否覆盖"/tmp/test/data/test3.txt"? n
cp:是否覆盖"/tmp/test/data/test4.txt"? n
[root@centos7-02 /]# \cp -r /data/ /tmp/test/
[root@centos7-02 /]# tree /tmp/test/
/tmp/test/
├── data
│ ├── test1.txt
│ ├── test2.txt
│ ├── test3.txt
│ └── test4.txt
└── test1.txt
1 directory, 5 files
[root@centos7-02 /]#
特殊符号
Linux 系统中,“!” 符号或者操做符一般被用作逻辑否认的操做符,同时也经过一些调整和改动命令来从历史记录中找出你须要的命令行。
关于!几个神奇操做用法:
1.从历史记录中使用命令号来运行命令:
例如:
[root@centos7-02 /]# history
254 history
255 ! $253
256 history
257 ls /tmp/test/
258 history
[root@centos7-02 /]# !257
ls /tmp/test/
data test1.txt test2.txt
[root@centos7-02 /]#
2.执行指定的以前执行过的命令
例如:
246 tree /tmp/test/
247 cp /data/test2.txt /tmp/test/
248 echo /data/test2.txt
249 echo /tmp/test/
250 /tmp/test/
251 ls /tmp/test/
252 cp /data/test2.txt /tmp/test/
253 ls /tmp/test/
254 history
255 ! $253
256 history
257 ls /tmp/test/
258 history
259 ls /tmp/test/
260 history
261 ls /tmp/test/
262 his
263 history
[root@centos7-02 /]# !-12
cp /data/test2.txt /tmp/test/
cp:是否覆盖"/tmp/test/test2.txt"? n
[root@centos7-02 /]#
3.向一条新命令传递旧命令的参数避免重复输入(取最后一个参数)
例如:
[root@centos7-02 /]# tree /tmp/test/
/tmp/test/
├── data
│ ├── test1.txt
│ ├── test2.txt
│ ├── test3.txt
│ └── test4.txt
├── test1.txt
└── test2.txt
1 directory, 6 files
[root@centos7-02 /]# ls !$
ls /tmp/test/
data test1.txt test2.txt
[root@centos7-02 /]#
[root@centos7-02 /]# ls /tmp/test/
data test1.txt test2.txt
[root@centos7-02 /]# tree !$
tree /tmp/test/
/tmp/test/
├── data
│ ├── test1.txt
│ ├── test2.txt
│ ├── test3.txt
│ └── test4.txt
├── test1.txt
└── test2.txt
1 directory, 6 files
[root@centos7-02 /]# cp /data/test4.txt /tmp/test/
[root@centos7-02 /]# ls !$
ls /tmp/test/
data test1.txt test2.txt test4.txt
[root@centos7-02 /]#
4.用!处理两个以上的参数:
[root@centos7-02 /]# cp /data/test4.txt /tmp/test/
cp:是否覆盖"/tmp/test/test4.txt"? y
[root@centos7-02 /]# ls !$
ls /tmp/test/
data test1.txt test2.txt test4.txt
[root@centos7-02 /]# ls !cp:1
ls /data/test4.txt
/data/test4.txt
[root@centos7-02 /]# ls !cp:2
ls /tmp/test/
data test1.txt test2.txt test4.txt
[root@centos7-02 /]#
[root@centos7-02 /]# cp /data/test4.txt /tmp/test/
cp:是否覆盖"/tmp/test/test4.txt"? y
[root@centos7-02 /]# ls !^
ls /data/test4.txt
/data/test4.txt
[root@centos7-02 /]# cp /data/test4.txt /tmp/test/
cp:是否覆盖"/tmp/test/test4.txt"? y
[root@centos7-02 /]# ls !$
ls /tmp/test/
data test1.txt test2.txt test4.txt
[root@centos7-02 /]#
5.经过关键词来执行以前的命令:
[root@centos7-02 /]# !ls
ls /tmp/test/
data test1.txt test2.txt test4.txt
[root@centos7-02 /]# !cp
cp /data/test4.txt /tmp/test/
cp:是否覆盖"/tmp/test/test4.txt"? y
[root@centos7-02 /]#