cp 拷貝文件或目錄
默認不能拷貝目錄
常用來備份;
[root@MongoDB ~]# cp a.txt /tmp/
[root@MongoDB ~]# cp /root/a.txt /tmp/ cp: overwrite ‘/tmp/a.txt’? y
提示是否覆蓋,首先考慮到有別名 -i 在覆蓋前提示
cp命令默認是不會提示overwrtite的,但是cp的-i選項會提示,而一般linux的用戶環境文件~/.bashrc中會把cp命名成alias
cp='cp -i' 如:
[root@MongoDB ~]# alias |grep cp alias cp='cp -i'
這樣在linux下輸入cp命令實際上運行的是cp -i,加上一個"\" 符號或者寫cp全路徑/bin/cp就是讓此次的cp命令不使用別名(cp -i)運行
拷貝覆蓋不提示,默認是提示的
第一種方法:\cp
\cp /mnt/test.txt /tmp
第二種方法:cp加 命令全路徑
/bin/cp /mnt/test.txt /tmp
屏蔽掉系統默認的對應的命令別名,默認執行cp的操作調用了別名,所以會提示覆蓋
復制的文件時間改變了
[root@MongoDB ~]# cp file1.txt file4.txt [root@MongoDB ~]# ll total 4 -rw-------. 1 root root 1851 Mar 27 08:38 anaconda-ks.cfg -rw-r--r-- 1 root root 0 May 18 07:41 file1.txt -rw-r--r-- 1 root root 0 May 18 07:41 file2.txt -rw-r--r-- 1 root root 0 May 18 07:41 file3.txt -rw-r--r-- 1 root root 0 May 18 07:42 file4.txt
-a 保持屬性不變
[root@MongoDB ~]# cp -a file1.txt file4.txt [root@MongoDB ~]# ll total 4 -rw-------. 1 root root 1851 Mar 27 08:38 anaconda-ks.cfg -rw-r--r-- 1 root root 0 May 18 07:41 file1.txt -rw-r--r-- 1 root root 0 May 18 07:41 file2.txt -rw-r--r-- 1 root root 0 May 18 07:41 file3.txt -rw-r--r-- 1 root root 0 May 18 07:41 file4.txt
-r 拷貝目錄
也可以使用-a 拷貝目錄 -a 等效於 -pdr 參數
-p 連同屬性一同復制過去,權限等
[root@MongoDB ~]# cp -a dir{3,4} [root@MongoDB ~]# ll total 4 -rw-------. 1 root root 1851 Mar 27 08:38 anaconda-ks.cfg drwxr-xr-x 2 root root 6 May 18 07:54 dir1 drwxr-xr-x 2 root root 6 May 18 07:54 dir2 drwxr-xr-x 2 root root 6 May 18 07:54 dir3 drwxr-xr-x 2 root root 6 May 18 07:54 dir4