星空网 > 软件开发 > 操作系统

cp命令实验

 

创建条件

[root@localhost ~]#mkdir /source[root@localhost ~]#mkdir /target[root@localhost ~]#cp /etc/l*.conf /source[root@localhost ~]#ll /sourcetotal 20-rw-r--r--. 1 root root  28 Aug 10 09:24 ld.so.conf-rw-r-----. 1 root root 191 Aug 10 09:24 libaudit.conf-rw-r--r--. 1 root root 2391 Aug 10 09:24 libuser.conf-rw-r--r--. 1 root root  19 Aug 10 09:24 locale.conf-rw-r--r--. 1 root root 662 Aug 10 09:24 logrotate.conf

 

第一种:源文件复制,目标文件不存在

[root@localhost ~]#cp /source/locale.conf /target/file1[root@localhost /target]#lltotal 4-rw-r--r--. 1 root root 19 Aug 10 10:31 file1

可以复制,复制的目标文件的目录必须存在(不然会报错),复制后文件相同,表示复制到目录后将文件重命名

当目标文件的目录(/dir)不存在时,会报错

[root@localhost /target]#cp /source/libaudit.conf /dir/file1cp: cannot create regular file ‘/dir/file1’: No such file or directory

 

第二种:源文件复制,目标文件存在

[root@localhost /target]#lltotal 4-rw-r--r--. 1 root root 19 Aug 10 09:33 file1[root@localhost /target]#cp /source/libaudit.conf /target/file1cp: overwrite ‘/target/file1’? y[root@localhost /target]#lltotal 4-rw-r--r--. 1 root root 191 Aug 10 09:39 file1[root@localhost /target]#cat file1# This is the configuration file for libaudit tunables.# It is currently only used for the failure_action tunable.# failure_action can be: log, ignore, terminatefailure_action = ignore

可以复制,当有文件名相同表明覆盖,会提示,查看文件内容,表明确实会覆盖原文件

 

第三种:源文件复制,目标文件存在且为目录,

[root@localhost /target]#rm -f *[root@localhost /target]#lltotal 0[root@localhost /target]#mkdir file1[root@localhost /target]#lltotal 0drwxr-xr-x. 2 root root 6 Aug 10 09:44 file1[root@localhost /target]#cp /source/locale.conf /target/file1[root@localhost /target]#lltotal 0drwxr-xr-x. 2 root root 24 Aug 10 09:46 file1[root@localhost /target]#ll file1total 4-rw-r--r--. 1 root root 19 Aug 10 09:46 locale.conf

可以复制,将文件复制到同名file1目录下面

 

第四种:多文件复制,目标文件不存在

[root@localhost /target]#lltotal 0[root@localhost /target]#cp /source/* /target/file1cp: target ‘/target/file1’ is not a directory

复制错误,目标必须要为目录才可以

 

第五种:多文件复制,目标文件存在

[root@localhost /target]#cp /etc/fstab /target/file1[root@localhost /target]#lltotal 4-rw-r--r--. 1 root root 595 Aug 10 10:04 file1[root@localhost /target]#cp /source/* /target/file1cp: target ‘/target/file1’ is not a directory

复制错误,目标必须要为目录才可以

 

第六种:多文件复制,目标文件存在为目录

[root@localhost /target]#lltotal 0[root@localhost /target]#mkdir file1[root@localhost /target]#lltotal 0drwxr-xr-x. 2 root root 6 Aug 10 10:07 file1[root@localhost /target]#cp /source/* /target/file1[root@localhost /target]#lltotal 0drwxr-xr-x. 2 root root 101 Aug 10 10:08 file1[root@localhost /target]#ll file1total 20-rw-r--r--. 1 root root  28 Aug 10 10:08 ld.so.conf-rw-r-----. 1 root root 191 Aug 10 10:08 libaudit.conf-rw-r--r--. 1 root root 2391 Aug 10 10:08 libuser.conf-rw-r--r--. 1 root root  19 Aug 10 10:08 locale.conf-rw-r--r--. 1 root root 662 Aug 10 10:08 logrotate.conf

复制所有文件到目标目录中

 

第七种:目录复制,必须使用-r选项,递归复制,当目标文件不存在时(目标文件的上一级目录必须存在)

[root@localhost /target]#ll /targettotal 0[root@localhost /target]#cp -r /source /target/file1[root@localhost /target]#lltotal 0drwxr-xr-x. 2 root root 101 Aug 10 10:16 file1[root@localhost /target]#ll file1/total 20-rw-r--r--. 1 root root  28 Aug 10 10:16 ld.so.conf-rw-r-----. 1 root root 191 Aug 10 10:16 libaudit.conf-rw-r--r--. 1 root root 2391 Aug 10 10:16 libuser.conf-rw-r--r--. 1 root root  19 Aug 10 10:16 locale.conf-rw-r--r--. 1 root root 662 Aug 10 10:16 logrotate.conf

可以复制,将原目录下的所有文件复制到目标目录中

 

第八种:目录复制,必须使用-r选项,递归复制,当目标存在且为文件时

[root@localhost /target]#lltotal 4-rw-r--r--. 1 root root 595 Aug 10 10:20 file1[root@localhost /target]#cp -r /source /target/file1cp: cannot overwrite non-directory ‘/target/file1’ with directory ‘/source’

复制错误,必须是目录

 

第九种:目录复制,必须使用-r选项,递归复制,当目标存在且为目录时

[root@localhost /target]#rm -f *[root@localhost /target]#lltotal 0[root@localhost /target]#mkdir file1[root@localhost /target]#lltotal 0drwxr-xr-x. 2 root root 6 Aug 10 10:22 file1[root@localhost /target]#cp -r /source /target/file1[root@localhost /target]#ll total 0drwxr-xr-x. 3 root root 19 Aug 10 10:23 file1[root@localhost /target]#ll file1/total 0drwxr-xr-x. 2 root root 101 Aug 10 10:23 source[root@localhost /target]#ll file1/sourcetotal 20-rw-r--r--. 1 root root  28 Aug 10 10:23 ld.so.conf-rw-r-----. 1 root root 191 Aug 10 10:23 libaudit.conf-rw-r--r--. 1 root root 2391 Aug 10 10:23 libuser.conf-rw-r--r--. 1 root root  19 Aug 10 10:23 locale.conf-rw-r--r--. 1 root root 662 Aug 10 10:23 logrotate.conf

可以复制,复制原目录及下面的所有内容到目标目录下面

 

cp命令实验images/loading.gif' data-original="http://images2015.cnblogs.com/blog/853909/201608/853909-20160810103929387-1800362484.png" />

 




原标题:cp命令实验

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流