1 9 複製檔案 cp命令

2021-09-26 01:58:13 字數 4243 閱讀 1930

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

12123

[root@hadoop tmp]# cat test_file2

12123

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

關於硬鏈結檔案,詳見:

[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

關於硬鏈結詳見

關於軟鏈結檔案,詳見:

[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

Linux 中 cp 命令(檔案複製)

cp命令用來將乙個或多個原始檔或者目錄複製到指定的目的檔案或目錄。它可以將單個原始檔複製成乙個指定檔名的具體的檔案或乙個已經存在的目錄下。cp命令還支援同時複製多個檔案,當一次複製多個檔案時,目標檔案引數必須是乙個已經存在的目錄,否則將出現錯誤。語法cp 選項 引數 選項 a 此引數的效果和同時指定...

複製檔案或目錄命令 cp

1 命令名稱 cp 2 英文原意 copy 3 命令所在路徑 bin cp 4 執行許可權 所有使用者 5 功能描述 複製檔案或目錄 語法 cp rp 原檔案或目錄 目標目錄 r 複製目錄 p 保留檔案屬性 例子 比如我們將 root 目錄下的install.log 檔案複製到 tmp 目錄下 可以...

Linux 中 cp 命令(檔案複製)

cp命令用來將乙個或多個原始檔或者目錄複製到指定的目的檔案或目錄。它可以將單個原始檔複製成乙個指定檔名的具體的檔案或乙個已經存在的目錄下。cp命令還支援同時複製多個檔案,當一次複製多個檔案時,目標檔案引數必須是乙個已經存在的目錄,否則將出現錯誤。cp 選項 引數 a 此引數的效果和同時指定 dpr ...