cp命令

cp命令

cp=copy 语法:cp [选项] [ 源文件 ] [目标文件]

1.目标文件存在,会提示是否覆盖,并且更改源文件名为目标文件名

[root@xxlinux-02 ~]# mkdir /tmp/xxlinux/01/
[root@xxlinux-02 ~]# touch /tmp/xxlinux/01/1.txt
[root@xxlinux-02 ~]# cp /etc/passwd /tmp/xxlinux/01/1.txt
cp:是否覆盖"/tmp/xxlinux/01/1.txt"? y
[root@xxlinux-02 ~]# tree /tmp/xxlinux/01
/tmp/xxlinux/01
└── 1.txt

0 directories, 1 file
[root@xxlinux-02 ~]# 

目标文件不存在,直接以目标文件为名拷贝

[root@xxlinux-02 ~]# cp /etc/passwd /tmp/xxlinux/01/passwd
[root@xxlinux-02 ~]# tree /tmp/xxlinux/01
/tmp/xxlinux/01
├── 1.txt
└── passwd

0 directories, 2 files

2.如果拷贝目录,需要加参数 “-r”

当目标目录不存在时,直接拷贝并以目标目录命名:

[root@xxlinux-02 ~]# cp -r /tmp/xxlinux/01/ /tmp/xxlinux/02/
[root@xxlinux-02 ~]# tree /tmp/xxlinux/
/tmp/xxlinux/
├── 01
│   ├── 1.txt
│   ├── passwd
│   └── passwd01
└── 02
    ├── 1.txt
    ├── passwd
    └── passwd01

2 directories, 6 files

当目标目录存在时,会将源目录拷贝到目标目录下:

[root@xxlinux-02 ~]# cp -r /tmp/xxlinux/01/ /tmp/xxlinux/02/
[root@xxlinux-02 ~]# tree /tmp/xxlinux/
/tmp/xxlinux/
├── 01
│   ├── 1.txt
│   ├── passwd
│   └── passwd01
└── 02
    ├── 1.txt
    ├── passwd
    └── passwd01

2 directories, 6 files
[root@xxlinux-02 ~]# cp -r /tmp/xxlinux/01/ /tmp/xxlinux/02/
[root@xxlinux-02 ~]# tree /tmp/xxlinux/
/tmp/xxlinux/
├── 01
│   ├── 1.txt
│   ├── passwd
│   └── passwd01
└── 02
    ├── 01
    │   ├── 1.txt
    │   ├── passwd
    │   └── passwd01
    ├── 1.txt
    ├── passwd
    └── passwd01

3 directories, 9 files
posted @ 2017-08-30 19:56  指环王Raul  阅读(197)  评论(0编辑  收藏  举报