cp命令實驗,cp命令

來源:互聯網
上載者:User

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

可以複製,複製原目錄及下面的所有內容到目標目錄下面

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.