Linux-CP命令

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 /]# 

转载于:https://my.oschina.net/u/3866154/blog/1823292

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值