cp(取copy之意)命令将文件或目录从文件系统的一个位置复制到另一个位置。
cp命令最基本的形式是使用两个参数:源对象和目标对象:
cp source destination
cp命令的用法如下:
1、用法一:复制文件,cp 源文件名 目标文件名
如果source和destination参数都是文件名,cp命令将源文件复制为一个新文件,使用destination参数指定文件名。
新文件和源文件具有相同的文件内容
新文件和源文件是不同的文件,具有不同的innode编号,从ls -li就可看出
新文件具有自己的文件创建时间和最后修改时间
新文件和源文件都在同一级目录下
[root@hadoop tmp]# cp test_file1 test_file2
[root@hadoop tmp]# ls -li test_file?
16811483 -rw-r--r--. 1 root root 7 8月 10 18:48 test_file1
16811794 -rw-r--r--. 1 root root 7 8月 10 18:48 test_file2
[root@hadoop tmp]# cat test_file1
12
123
[root@hadoop tmp]# cat test_file2
12
123
2、用法二:cp -i 源文件名 目标文件名,如果目标文件名已经存在,cp命令提示是否覆盖文件
[root@hadoop tmp]# cp -i test_file1 test_file2
cp:是否覆盖"test_file2"? y
[root@hadoop tmp]# ls -li test_file?
16811483 -rw-r--r--. 1 root root 7 8月 10 18:48 test_file1
16811794 -rw-r--r--. 1 root root 7 8月 10 18:54 test_file2
3、用法三:将文件复制到其他目录下,不修改文件名
[root@hadoop tmp]# cp test_file1 dafu1/
[root@hadoop tmp]# cd dafu1
[root@hadoop dafu1]# ls -l
总用量 4
-rw-r--r--. 1 root root 0 8月 10 16:50 file1
-rw-r--r--. 1 root root 0 8月 10 16:50 file2
-rw-r--r--. 1 root root 0 8月 10 16:50 file3
-rw-r--r--. 1 root root 7 8月 10 18:57 test_file1
4、用法四:将文件复制到其他目录下,同时修改文件名
[root@hadoop tmp]# cp test_file2 dafu1/test_file2_new
[root@hadoop tmp]# cd dafu1
[root@hadoop dafu1]# ll
总用量 8
-rw-r--r--. 1 root root 0 8月 10 16:50 file1
-rw-r--r--. 1 root root 0 8月 10 16:50 file2
-rw-r--r--. 1 root root 0 8月 10 16:50 file3
-rw-r--r--. 1 root root 7 8月 10 18:57 test_file1
-rw-r--r--. 1 root root 7 8月 10 18:59 test_file2_new
5、用法五:cp -p参数可以使复制文件的访问时间和修改时间与源文件一致
-p参数保留文件属性,但是仍然是不同的文件,目标文件与源文件仍然具有不同的inode节点
[root@hadoop tmp]# cp -p test_file1 test_file3
[root@hadoop tmp]# ls -li test_file?
16811483 -rw-r--r--. 1 root root 7 8月 10 18:48 test_file1
16811794 -rw-r--r--. 1 root root 7 8月 10 18:56 test_file2
16811795 -rw-r--r--. 1 root root 7 8月 10 18:48 test_file3
上面都是复制文件,cp命令还可以实现复制整个目录。
6、用法六:cp -R 源目录 目标目录,将源目录复制到目标目录下
[root@hadoop tmp]# cd dafu1
[root@hadoop dafu1]# ll
总用量 8
-rw-r--r--. 1 root root 0 8月 10 16:50 file1
-rw-r--r--. 1 root root 0 8月 10 16:50 file2
-rw-r--r--. 1 root root 0 8月 10 16:50 file3
-rw-r--r--. 1 root root 7 8月 10 18:57 test_file1
-rw-r--r--. 1 root root 7 8月 10 18:59 test_file2_new
[root@hadoop dafu1]# cd ..
[root@hadoop tmp]# cp -R dafu1 dafu3
[root@hadoop tmp]# cd dafu3
[root@hadoop dafu3]# ll
总用量 0
drwxr-xr-x. 2 root root 85 8月 10 19:08 dafu1
7、用法七:cp -R 源目录文件名 目标目录,将源目录下的文件复制到目标目录下
cp命令还可以和通配符配合使用
[root@hadoop tmp]# cp dafu1/file* dafu4/
[root@hadoop tmp]# cd dafu4
[root@hadoop dafu4]# ll
总用量 0
-rw-r--r--. 1 root root 0 8月 10 19:10 file1
-rw-r--r--. 1 root root 0 8月 10 19:10 file2
-rw-r--r--. 1 root root 0 8月 10 19:10 file3
8、用法八:cp -l 源文件 硬链接文件,创建源文件的硬链接文件
关于硬链接文件,详见:https://blog.csdn.net/yaoyelinger0912/article/details/99128562
[root@hadoop tmp]# cp -l test_file1 test_file4
[root@hadoop tmp]# ls -li test*
16811483 -rw-r--r--. 2 root root 7 8月 10 18:48 test_file1
16811794 -rw-r--r--. 1 root root 7 8月 10 18:56 test_file2
16811795 -rw-r--r--. 1 root root 7 8月 10 18:48 test_file3
16811483 -rw-r--r--. 2 root root 7 8月 10 18:48 test_file4
关于硬链接详见
9、用法九:cp -s 源文件 软链接文件,创建源文件的软链接文件
关于软链接文件,详见:https://blog.csdn.net/yaoyelinger0912/article/details/99128562
[root@hadoop tmp]# cp -s test_file1 test_file5
[root@hadoop tmp]# ls -li test_file*
16811483 -rw-r--r--. 2 root root 7 8月 10 18:48 test_file1
16811794 -rw-r--r--. 1 root root 7 8月 10 18:56 test_file2
16811795 -rw-r--r--. 1 root root 7 8月 10 18:48 test_file3
16811483 -rw-r--r--. 2 root root 7 8月 10 18:48 test_file4
16811796 lrwxrwxrwx. 1 root root 10 8月 10 23:53 test_file5 -> test_file1
与大部分命令
一样,cp命令也有一个可以提供帮助的命令行参数,如下: