歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> 學習Linux >> cp命令實驗,cp命令

cp命令實驗,cp命令

日期:2017/3/6 9:23:28   编辑:學習Linux

cp命令實驗,cp命令


cp命令實驗,cp命令


創建條件

[root@localhost ~]#mkdir /source
[root@localhost ~]#mkdir /target
[root@localhost ~]#cp /etc/l*.conf /source
[root@localhost ~]#ll /source
total 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]#ll
total 4
-rw-r--r--. 1 root root 19 Aug 10 10:31 file1

可以復制,復制的目標文件的目錄必須存在(不然會報錯),復制後文件相同,表示復制到目錄後將文件重命名

當目標文件的目錄(/dir)不存在時,會報錯

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

第二種:源文件復制,目標文件存在

[root@localhost /target]#ll
total 4
-rw-r--r--. 1 root root 19 Aug 10 09:33 file1
[root@localhost /target]#cp /source/libaudit.conf /target/file1
cp: overwrite ‘/target/file1’? y
[root@localhost /target]#ll
total 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, terminate
failure_action = ignore

可以復制,當有文件名相同表明覆蓋,會提示,查看文件內容,表明確實會覆蓋原文件

第三種:源文件復制,目標文件存在且為目錄,

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

可以復制,將文件復制到同名file1目錄下面

第四種:多文件復制,目標文件不存在

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

復制錯誤,目標必須要為目錄才可以

第五種:多文件復制,目標文件存在

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

復制錯誤,目標必須要為目錄才可以

第六種:多文件復制,目標文件存在為目錄

[root@localhost /target]#ll
total 0
[root@localhost /target]#mkdir file1
[root@localhost /target]#ll
total 0
drwxr-xr-x. 2 root root 6 Aug 10 10:07 file1
[root@localhost /target]#cp /source/* /target/file1
[root@localhost /target]#ll
total 0
drwxr-xr-x. 2 root root 101 Aug 10 10:08 file1
[root@localhost /target]#ll file1
total 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 /target
total 0
[root@localhost /target]#cp -r /source /target/file1
[root@localhost /target]#ll
total 0
drwxr-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]#ll
total 4
-rw-r--r--. 1 root root 595 Aug 10 10:20 file1
[root@localhost /target]#cp -r /source /target/file1
cp: cannot overwrite non-directory ‘/target/file1’ with directory ‘/source’

復制錯誤,必須是目錄

第九種:目錄復制,必須使用-r選項,遞歸復制,當目標存在且為目錄時

[root@localhost /target]#rm -f *
[root@localhost /target]#ll
total 0
[root@localhost /target]#mkdir file1
[root@localhost /target]#ll
total 0
drwxr-xr-x. 2 root root 6 Aug 10 10:22 file1
[root@localhost /target]#cp -r /source /target/file1
[root@localhost /target]#ll 
total 0
drwxr-xr-x. 3 root root 19 Aug 10 10:23 file1
[root@localhost /target]#ll file1/
total 0
drwxr-xr-x. 2 root root 101 Aug 10 10:23 source
[root@localhost /target]#ll file1/source
total 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

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

http://xxxxxx/Linuxjc/1148634.html TechArticle

Copyright © Linux教程網 All Rights Reserved