cp命令實驗,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
可以複製,複製原目錄及下面的所有內容到目標目錄下面