Linux常用基本命令[cp]


cp:復制文件或者目錄

用法格式:

cp [option] [source] [dest]

cp [選項] [源文件] [目標文件]

>用root賬戶,創建文件,復制文件

root@dev:/home/ghostwu/linux/cp# vim 1.txt 
root@dev:/home/ghostwu/linux/cp# ls -l
total 4
-rw-r--r-- 1 root root 19 5月   6 17:47 1.txt
root@dev:/home/ghostwu/linux/cp# cp 1.txt 2.txt
root@dev:/home/ghostwu/linux/cp# ls -l
total 8
-rw-r--r-- 1 root root 19 5月   6 17:47 1.txt
-rw-r--r-- 1 root root 19 5月   6 17:48 2.txt
root@dev:/home/ghostwu/linux/cp# su - ghostwu
ghostwu@dev:~$ cd -
-su: cd: OLDPWD not set
ghostwu@dev:~$ cd linux/cp
ghostwu@dev:~/linux/cp$ ls -l
total 8
-rw-r--r-- 1 root root 19 5月   6 17:47 1.txt
-rw-r--r-- 1 root root 19 5月   6 17:48 2.txt
ghostwu@dev:~/linux/cp$ cp 2.txt 3.txt
cp: cannot create regular file '3.txt': Permission denied

上面,當我切換到ghostwu這個賬戶去復制的時候,權限不允許,因為2.txt 這個文件 的其他組只有 只讀 權限, 而cp需要寫權限,所以就報了一個無權限創建復制的文件。

方法一,用sudo提權

ghostwu@dev:~/linux/cp$ ls -l
total 8
-rw-r--r-- 1 root root 19 5月   6 17:47 1.txt
-rw-r--r-- 1 root root 19 5月   6 17:48 2.txt
ghostwu@dev:~/linux/cp$ sudo cp 2.txt 3.txt
[sudo] password for ghostwu: 
ghostwu@dev:~/linux/cp$ ls -l
total 12
-rw-r--r-- 1 root root 19 5月   6 17:47 1.txt
-rw-r--r-- 1 root root 19 5月   6 17:48 2.txt
-rw-r--r-- 1 root root 19 5月   6 17:52 3.txt

方法二,用root用戶給文件的其他組用戶 可寫權限,同時普通用戶要對文件所屬的目錄擁有寫權限, 也就是要對 "cp" 這個目錄擁有寫權限

ghostwu@dev:~/linux$ ls -l
total 4
drwxr-xr-x 2 root root 4096 5月   6 17:52 cp
ghostwu@dev:~/linux$ sudo chmod o+w cp
ghostwu@dev:~/linux$ ls -l
total 4
drwxr-xrwx 2 root root 4096 5月   6 17:52 cp
ghostwu@dev:~/linux$ cd cp
ghostwu@dev:~/linux/cp$ ls -l
total 12
-rw-r--r-- 1 root root 19 5月   6 17:47 1.txt
-rw-r--r-- 1 root root 19 5月   6 17:48 2.txt
-rw-r--rw- 1 root root 19 5月   6 17:52 3.txt
ghostwu@dev:~/linux/cp$ sudo chmod o+w 2.txt 
ghostwu@dev:~/linux/cp$ ls -l
total 12
-rw-r--r-- 1 root root 19 5月   6 17:47 1.txt
-rw-r--rw- 1 root root 19 5月   6 17:48 2.txt
-rw-r--rw- 1 root root 19 5月   6 17:52 3.txt
ghostwu@dev:~/linux/cp$ cp 2.txt 4.txt
ghostwu@dev:~/linux/cp$ ls -l
total 16
-rw-r--r-- 1 root    root    19 5月   6 17:47 1.txt
-rw-r--rw- 1 root    root    19 5月   6 17:48 2.txt
-rw-r--rw- 1 root    root    19 5月   6 17:52 3.txt
-rw-r--r-- 1 ghostwu ghostwu 19 5月   6 17:58 4.txt

用普通用戶去復制root賬戶創建的2.txt文件,起一個新名字4.txt,默認情況下cp 改變了文件的權限和時間屬性,如果在復制的時候想保留文件原有的權限信息以及時間屬性時,可以加參數 -p

ghostwu@dev:~/linux/cp$ ls -l
total 16
-rw-r--r-- 1 root    root    19 5月   6 17:47 1.txt
-rw-r--rw- 1 root    root    19 5月   6 17:48 2.txt
-rw-r--rw- 1 root    root    19 5月   6 17:52 3.txt
-rw-r--r-- 1 ghostwu ghostwu 19 5月   6 17:58 4.txt
ghostwu@dev:~/linux/cp$ cp -p 2.txt 5.txt
ghostwu@dev:~/linux/cp$ ls -l
total 20
-rw-r--r-- 1 root    root    19 5月   6 17:47 1.txt
-rw-r--rw- 1 root    root    19 5月   6 17:48 2.txt
-rw-r--rw- 1 root    root    19 5月   6 17:52 3.txt
-rw-r--r-- 1 ghostwu ghostwu 19 5月   6 17:58 4.txt
-rw-r--rw- 1 ghostwu ghostwu 19 5月   6 17:48 5.txt

-i: 帶提示信息的復制,默認情況下,cp命令會直接覆蓋

ghostwu@dev:~/linux/cp$ ls -l
total 20
-rw-r--r-- 1 root    root    19 5月   6 17:47 1.txt
-rw-r--rw- 1 root    root    19 5月   6 17:48 2.txt
-rw-r--rw- 1 root    root    19 5月   6 17:52 3.txt
-rw-r--r-- 1 ghostwu ghostwu 19 5月   6 17:58 4.txt
-rw-r--rw- 1 ghostwu ghostwu 19 5月   6 17:48 5.txt
ghostwu@dev:~/linux/cp$ cp 2.txt 5.txt 
ghostwu@dev:~/linux/cp$ cp -i 2.txt 5.txt 
cp: overwrite '5.txt'? y

-r參數: 遞歸復制目錄以及文件

ghostwu@dev:~/linux/cp$ ls -l
total 20
-rw-r--r-- 1 root    root    19 5月   6 17:47 1.txt
-rw-r--rw- 1 root    root    19 5月   6 17:48 2.txt
-rw-r--rw- 1 root    root    19 5月   6 17:52 3.txt
-rw-r--r-- 1 ghostwu ghostwu 19 5月   6 17:58 4.txt
-rw-r--rw- 1 ghostwu ghostwu 19 5月   6 18:04 5.txt
ghostwu@dev:~/linux/cp$ mkdir -p a/b
ghostwu@dev:~/linux/cp$ mv *.txt a/b/
ghostwu@dev:~/linux/cp$ tree
.
└── a
    └── b
        ├── 1.txt
        ├── 2.txt
        ├── 3.txt
        ├── 4.txt
        └── 5.txt

2 directories, 5 files
ghostwu@dev:~/linux/cp$ cp a a2
cp: omitting directory 'a'
ghostwu@dev:~/linux/cp$ ls
a
ghostwu@dev:~/linux/cp$ cp -r a a2
ghostwu@dev:~/linux/cp$ tree
.
├── a
│   └── b
│       ├── 1.txt
│       ├── 2.txt
│       ├── 3.txt
│       ├── 4.txt
│       └── 5.txt
└── a2
    └── b
        ├── 1.txt
        ├── 2.txt
        ├── 3.txt
        ├── 4.txt
        └── 5.txt

4 directories, 10 files
ghostwu@dev:~/linux/cp$ 

通過alias別名,給cp命令加提示信息

ghostwu@dev:~/linux/cp$ alias cp='cp -i'
ghostwu@dev:~/linux/cp$ ls
a  a2
ghostwu@dev:~/linux/cp$ touch 1.txt
ghostwu@dev:~/linux/cp$ cp 1.txt 2.txt
ghostwu@dev:~/linux/cp$ cp 1.txt 2.txt 
cp: overwrite '2.txt'? y
ghostwu@dev:~/linux/cp$ 

使用命令的絕對路徑(全路徑),可以屏蔽別名

ghostwu@dev:~/linux/cp$ alias | grep cp
alias cp='cp -i'
ghostwu@dev:~/linux/cp$ ls
1.txt  2.txt  a  a2
ghostwu@dev:~/linux/cp$ cp 1.txt 2.txt 
cp: overwrite '2.txt'? y
ghostwu@dev:~/linux/cp$ which cp
/bin/cp
ghostwu@dev:~/linux/cp$ /bin/cp 1.txt 2.txt 

使用反斜杠,也可以屏蔽系統別名

ghostwu@dev:~/linux/cp$ \cp 1.txt 2.txt 
ghostwu@dev:~/linux/cp$ \cp 2.txt 1.txt 

-a參數,相當於-r -d -p三個參數的綜合作用效果

ghostwu@dev:~/linux/cp$ ls
1.txt  2.txt  a  a2
ghostwu@dev:~/linux/cp$ cp -a a a3
ghostwu@dev:~/linux/cp$ ls
1.txt  2.txt  a  a2  a3

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM