cp命令用来复制文件或者目录
命令格式:
cp [选项] ... 源 ..... 目的
命令功能:
将源文件复制到目标文件,或者将多个源文件复制到目标目录
命令实例:
(1)
复制单个文件到目标目录,文件在目标目录中不存在
[root@centos65 testorder]# ll
total 4
drwxr-xr-x 4 root root 31 Aug 19 09:54 test001
-rw-r--r-- 1 root root 18 Aug 19 09:58 test003.log
-rw-r--r-- 1 root root 0 Aug 19 09:57 test003.log~
[root@centos65 testorder]#
[root@centos65 testorder]#
[root@centos65 testorder]# cp test003.log test001
[root@centos65 testorder]#
[root@centos65 testorder]#
[root@centos65 testorder]# ll
total 4
drwxr-xr-x 4 root root 49 Aug 20 08:36 test001
-rw-r--r-- 1 root root 18 Aug 19 09:58 test003.log
-rw-r--r-- 1 root root 0 Aug 19 09:57 test003.log~
[root@centos65 testorder]#
[root@centos65 testorder]#
[root@centos65 testorder]#
[root@centos65 testorder]# cd test001
[root@centos65 test001]# ll
total 4
drwxr-xr-x 2 root root 6 Aug 19 09:55 test
drwxr-xr-x 2 root root 78 Aug 19 09:55 test001
-rw-r--r-- 1 root root 18 Aug 20 08:36 test003.log
(2)目标文件存在的时候,会询问是否覆盖
[root@centos65 testorder]# cd test001
[root@centos65 test001]# ll
total 4
drwxr-xr-x 2 root root 6 Aug 19 09:55 test
drwxr-xr-x 2 root root 78 Aug 19 09:55 test001
-rw-r--r-- 1 root root 18 Aug 20 08:36 test003.log
[root@centos65 test001]#
[root@centos65 test001]# cp test003.log test001
cp: overwrite `test001/test003.log'? y
(3)复制整个目录
[root@centos65 test001]# ll
total 4
drwxr-xr-x 2 root root 6 Aug 19 09:55 test
drwxr-xr-x 2 root root 78 Aug 19 09:55 test001
-rw-r--r-- 1 root root 18 Aug 20 08:36 test003.log
[root@centos65 test001]#
[root@centos65 test001]#
[root@centos65 test001]#
[root@centos65 test001]# cp -a test001 test
[root@centos65 test001]#
[root@centos65 test001]#
[root@centos65 test001]# cd test
[root@centos65 test]# ll
total 0
drwxr-xr-x 2 root root 78 Aug 19 09:55 test001
[root@centos65 test]# cd ../test001
[root@centos65 test001]# ll
total 8
-rw-r--r-- 1 root root 40 Aug 19 09:24 test001.log
-rw-r--r-- 1 root root 0 Aug 19 09:36 test002.log
-rw-r--r-- 1 root root 18 Aug 20 08:37 test003.log
-rw-r--r-- 1 root root 0 Aug 19 09:36 test004.log
(4)建立一个连接档(相当于windows的快捷方式)
[root@centos65 test]# ll
total 8
drwxr-xr-x 2 root root 78 Aug 19 09:55 test001
-rw-r--r-- 1 root root 40 Aug 20 08:47 test001.log
drwxr-xr-x 2 root root 6 Aug 20 08:43 test002
-rw-r--r-- 1 root root 0 Aug 20 08:47 test002.log
-rw-r--r-- 1 root root 18 Aug 20 08:47 test003.log
-rw-r--r-- 1 root root 0 Aug 20 08:47 test004.log
[root@centos65 test]#
[root@centos65 test]#
[root@centos65 test]# cp -s test001.log test001_link.log
[root@centos65 test]# ll
total 8
drwxr-xr-x 2 root root 78 Aug 19 09:55 test001
lrwxrwxrwx 1 root root 11 Aug 20 08:50 test001_link.log -> test001.log
-rw-r--r-- 1 root root 40 Aug 20 08:47 test001.log
drwxr-xr-x 2 root root 6 Aug 20 08:43 test002
-rw-r--r-- 1 root root 0 Aug 20 08:47 test002.log
-rw-r--r-- 1 root root 18 Aug 20 08:47 test003.log
-rw-r--r-- 1 root root 0 Aug 20 08:47 test004.log