欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

CentOS下cp命令中拷貝所有的寫法

  發(fā)布時(shí)間:2016-12-09 13:06:29   作者:佚名   我要評(píng)論
今天小編為大家?guī)?lái)了CentOS下cp命令中拷貝所有的寫法;希望可以幫助到大家!有需要的朋友可以過(guò)來(lái)看看

今天在編寫一個(gè)腳本的時(shí)候,發(fā)現(xiàn)一個(gè)比較奇怪的問(wèn)題:就是在使用cp拷貝當(dāng)前目錄下所有文件到目標(biāo)目錄的時(shí)候,源和目標(biāo)目錄大小不同。原來(lái)一直沒(méi)有留意有這樣的問(wèn)題,后來(lái)查了些資料,才知道以前一直使用的格式有誤。

一、預(yù)備

cp就是拷貝,最簡(jiǎn)單的使用方式就是:

cp oldfile newfile

但這樣只能拷貝文件,不能拷貝目錄,所以通常用:

cp -r old/ new/

那就會(huì)把old目錄整個(gè)拷貝到new目錄下。注意,不是把old目錄里面的文件拷貝到new目錄,而是把old直接拷貝到new下面,結(jié)果是:

引用

[root@dc5 test]# ll new/
total 4
drwxr-xr-x  2 root root 4096 Dec 15 11:55 old

那如果要保持源文件的所有權(quán)限,可以這樣:

cp -rp old/ new/

-p參數(shù),可以保持權(quán)限、宿主、時(shí)間棧,還可能包括link等;還有更簡(jiǎn)單的,就是用:

cp -a old/new/

-a參數(shù),就等于-dpR。

二、問(wèn)題1

好,我們來(lái)看看這次的問(wèn)題。環(huán)境是:

◎兩個(gè)目錄:old、new,其中old里面有個(gè)三個(gè)內(nèi)容:test1文件、test2目錄,還有就是.test3,這是一個(gè)隱含文件。

引用

[root@dc5 test]# ll -laR
.:
total 20
drwxr-xr-x  4 root root 4096 Dec 15 11:55 .
drwxrwxrwt  7 root root 4096 Dec 15 11:59 ..
drwxr-xr-x  2 root root 4096 Dec 15 12:14 new
drwxr-xr-x  3 root root 4096 Dec 15 12:14 old

./new:
total 8
drwxr-xr-x  2 root root 4096 Dec 15 12:14 .
drwxr-xr-x  4 root root 4096 Dec 15 11:55 ..

./old:
total 12
drwxr-xr-x  3 root root 4096 Dec 15 12:14 .
drwxr-xr-x  4 root root 4096 Dec 15 11:55 ..
-rw-r--r--  1 root root    0 Dec 15 12:07 .test3
-rw-r--r--  1 root root    0 Dec 15 12:05 test1
drwxr-xr-x  2 root root 4096 Dec 15 12:14 test2

./old/test2:
total 8
drwxr-xr-x  2 root root 4096 Dec 15 12:14 .
drwxr-xr-x  3 root root 4096 Dec 15 12:14 ..

◎操作一:

引用

[root@dc5 test]# cp -a old/* new/
[root@dc5 test]# ll -laR new/
new/:
total 12
drwxr-xr-x  3 root root 4096 Dec 15 12:15 .
drwxr-xr-x  4 root root 4096 Dec 15 11:55 ..
-rw-r--r--  1 root root    0 Dec 15 12:05 test1
drwxr-xr-x  2 root root 4096 Dec 15 12:14 test2

new/test2:
total 8
drwxr-xr-x  2 root root 4096 Dec 15 12:14 .
drwxr-xr-x  3 root root 4096 Dec 15 12:15 ..

問(wèn)題出來(lái)了:隱含的.test3文件沒(méi)有一齊拷貝到new目錄下。

原因是:*參數(shù)使用不正確。這樣的寫法,通常都是因?yàn)槭煜ち诉^(guò)去Dos的格式(包括我自己),而實(shí)際在bash環(huán)境下,cp使用*是不能匹配類似.開(kāi)頭的隱含文件的。

◎操作二

正確的寫法應(yīng)該這樣:

引用

[root@dc5 test]# cp -a old/. new/
[root@dc5 test]# ll -laR new/
new/:
total 12
drwxr-xr-x  3 root root 4096 Dec 15 12:14 .
drwxr-xr-x  4 root root 4096 Dec 15 11:55 ..
-rw-r--r--  1 root root    0 Dec 15 12:07 .test3
-rw-r--r--  1 root root    0 Dec 15 12:05 test1
drwxr-xr-x  2 root root 4096 Dec 15 12:14 test2

new/test2:
total 8
drwxr-xr-x  2 root root 4096 Dec 15 12:14 .
drwxr-xr-x  3 root root 4096 Dec 15 12:14 ..

不用*號(hào),而用.號(hào)代替。

還有一種比較復(fù)雜一些的寫法:

引用

[root@dc5 test]# cp -a old/* old/.[^.]* new/
[root@dc5 test]# ll -laR new/
new/:
total 12
drwxr-xr-x  3 root root 4096 Dec 15 12:25 .
drwxr-xr-x  4 root root 4096 Dec 15 11:55 ..
-rw-r--r--  1 root root    0 Dec 15 12:07 .test3
-rw-r--r--  1 root root    0 Dec 15 12:05 test1
drwxr-xr-x  2 root root 4096 Dec 15 12:14 test2

new/test2:
total 8
drwxr-xr-x  2 root root 4096 Dec 15 12:14 .
drwxr-xr-x  3 root root 4096 Dec 15 12:25 ..

請(qǐng)注意寫法,不要寫成.*了。(原因請(qǐng)看下面)

三、問(wèn)題2

上面提到不要寫成.*,那.*代表什么?

引用

[root@dc5 test]# echo .*
. ..

.*代表的是當(dāng)前目錄,以及上一層目錄。

所以,使用.*會(huì)導(dǎo)致更大的問(wèn)題:

引用

[root@dc5 test]# cp -a old/.* new/
cp: cannot copy a directory, `old/..', into itself, `new/'
cp: cannot copy a directory, `old/..', into itself, `new/'
cp: will not create hard link `new/old' to directory `new/.'
cp: overwrite `new/.test3'? y
[root@dc5 test]# ll -laR new/
new/:
total 16
drwxr-xr-x  4 root root 4096 Dec 15 11:55 .
drwxr-xr-x  4 root root 4096 Dec 15 11:55 ..
-rw-r--r--  1 root root    0 Dec 15 12:07 .test3
drwxr-xr-x  2 root root 4096 Dec 15 12:14 new
-rw-r--r--  1 root root    0 Dec 15 12:05 test1
drwxr-xr-x  2 root root 4096 Dec 15 12:14 test2

new/new:
total 8
drwxr-xr-x  2 root root 4096 Dec 15 12:14 .
drwxr-xr-x  4 root root 4096 Dec 15 11:55 ..
-rw-r--r--  1 root root    0 Dec 15 12:07 .test3
-rw-r--r--  1 root root    0 Dec 15 12:05 test1

new/test2:
total 8
drwxr-xr-x  2 root root 4096 Dec 15 12:14 .
drwxr-xr-x  4 root root 4096 Dec 15 11:55 ..

也就是說(shuō),使用.*就等于這樣了:

引用

[root@dc5 test]# cp -a old/. old/.. old/.test3 new/
[root@dc5 test]# echo old/.*
old/. old/.. old/.test3

四、擴(kuò)展

其實(shí)這樣的問(wèn)題,不單cp命令有這樣的問(wèn)題,在所有涉及含有特殊字符意義文件的命令時(shí),都需要考慮,例如rm:

引用

[root@dc5 new]# ll -a
total 12
drwxr-xr-x  3 root root 4096 Dec 15 12:14 .
drwxr-xr-x  4 root root 4096 Dec 15 11:55 ..
-rw-r--r--  1 root root    0 Dec 15 12:07 .test3
-rw-r--r--  1 root root    0 Dec 15 12:05 test1
drwxr-xr-x  2 root root 4096 Dec 15 12:14 test2
[root@dc5 new]# rm -rf *
[root@dc5 new]# ll -a
total 8
drwxr-xr-x  2 root root 4096 Dec 15 12:40 .
drwxr-xr-x  4 root root 4096 Dec 15 11:55 ..
-rw-r--r--  1 root root    0 Dec 15 12:07 .test3

正確的寫法應(yīng)該是:

引用

[root@dc5 new]# rm -rf .* *
rm: cannot remove `.' or `..'
rm: cannot remove `.' or `..'
[root@dc5 new]# ll -a
total 8
drwxr-xr-x  2 root root 4096 Dec 15 12:42 .
drwxr-xr-x  4 root root 4096 Dec 15 11:55 ..

當(dāng)然,這是一樣的:

引用

[root@dc5 new]# rm -rf * .[^.]*
[root@dc5 new]# ll -a
total 8
drwxr-xr-x  2 root root 4096 Dec 15 12:44 .
drwxr-xr-x  4 root root 4096 Dec 15 11:55 ..

※很多時(shí)候,預(yù)計(jì)的和實(shí)際的結(jié)果是完全不一樣的。bash編寫腳本尤其需要注意。

相關(guān)文章

  • CentOS中使用top命令查看CPU詳細(xì)圖解

    在系統(tǒng)維護(hù)的過(guò)程中,隨時(shí)可能有需要查看CPU 使用率,并根據(jù)相應(yīng)信息分析系統(tǒng)狀況的需要。在CentOS 中,可以通過(guò)top 命令來(lái)查看CPU 使用狀況,具體操作如下,感興趣的朋友
    2013-05-08
  • centos7怎么查看cpu內(nèi)存等系統(tǒng)性能參數(shù)?

    centos7怎么查看cpu內(nèi)存等系統(tǒng)性能參數(shù)?對(duì)centos系統(tǒng)不是很熟悉,想看看電腦的一些參數(shù),該怎么查看呢?下面我們就來(lái)看看詳細(xì)的查看教程,需要的朋友可以參考下
    2016-08-30
  • wdOS1.0(32、64位)CentOS服務(wù)器(集成lamp,lnmp,lnamp,wdcp安裝ISO)

    wdOS是一個(gè)基于CentOS版本精簡(jiǎn)優(yōu)化過(guò)的Linux服務(wù)器系統(tǒng),并集成nginx,apache,php,mysql等web應(yīng)用環(huán)境及wdcp管理系統(tǒng),裝好系統(tǒng),就可以通后臺(tái)管理服務(wù)器和網(wǎng)站,FTP,數(shù)據(jù)庫(kù)等,
    2013-07-28
  • Centos命令中nohup的用途的講解

    今天小編為大家?guī)?lái)的是Centos命令中nohup的用途的講解;希望對(duì)大家會(huì)有幫助;有需要的朋友可以過(guò)來(lái)看看
    2016-12-09
  • Centos系統(tǒng)用戶密碼字符串生成命令-shadow

    今天小編為大家?guī)?lái)的是Centos系統(tǒng)用戶密碼字符串生成命令-shadow;有需要的朋友可以過(guò)來(lái)看看,希望對(duì)大家的學(xué)習(xí)會(huì)有幫助
    2016-12-09
  • Centos文件搜索命令的講解

    今天小編就為大家?guī)?lái)Centos文件搜索命令的講解;希望對(duì)大家的學(xué)習(xí)會(huì)有幫助;有需要的朋友可以過(guò)來(lái)看看
    2016-12-09
  • centos下如何查看調(diào)優(yōu)apache狀態(tài)的相關(guān)命令

    一些朋友還不知道centos下如何查看調(diào)優(yōu)apache狀態(tài)的相關(guān)命令;下面小編就為大家?guī)?lái)centos下查看調(diào)優(yōu)apache狀態(tài)的相關(guān)命令的方法;有需要的朋友可以過(guò)來(lái)看看
    2016-12-09
  • Centos雙網(wǎng)卡bonding綁定實(shí)現(xiàn)負(fù)載均衡的方法

    一些朋友還不知道Centos雙網(wǎng)卡bonding綁定實(shí)現(xiàn)負(fù)載均衡的方法;下面小編就為大家?guī)?lái)Centos雙網(wǎng)卡bonding綁定實(shí)現(xiàn)負(fù)載均衡的方法;有需要的朋友過(guò)來(lái)看看吧
    2016-12-09
  • Centos下如何限制偽終端數(shù)?Centos下限制偽終端數(shù)的方法

    如果我們希望增加(或限制)通過(guò)ssh、telnet登錄服務(wù)的用戶,可以修改內(nèi)核參數(shù)來(lái)進(jìn)行限制;一些朋友還不知道Centos下如何限制偽終端數(shù)?下面小編就為大家?guī)?lái)Centos下限制
    2016-12-09
  • Centos中記錄終端輸出到文本文件的方法

    有些朋友還不知道Centos中如何記錄終端輸出到文本文件;下面小編就為大家?guī)?lái)Centos中記錄終端輸出到文本文件的方法;有需要的朋友可以過(guò)來(lái)看看
    2016-12-08

最新評(píng)論