作為一個(gè)不安穩(wěn)的小白,一天都在那路亂折騰,恰巧,老師課程題目中有一題將/etc/skles 這個(gè)目錄的文件除了..和. 復(fù)制到/home/USRNAEM 的家目錄下。然后自以為是不按照老師的方法,自己折騰用了這樣一條命令
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、小程序制作、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了阿克蘇免費(fèi)建站歡迎大家使用![root@local skel]# ls -A .bash_logout .bash_profile .bashrc [root@local skel]# cp $(ls -A) /home/mao1/ [root@local skel]# cd /home/mao1 [root@local mao1]# ls -A .bash_logout .bash_profile .bashrc
然后我又在這個(gè)skel目錄下建立一個(gè)目錄,確認(rèn)目錄是否能這樣被引用。
[root@local skel]# mkdir .test.dir [root@local skel]# mkdir test1.dir [root@local skel]# ls -A .bash_logout .bash_profile .bashrc test1.dir .test.dir [root@local skel]# cp -r $(ls -A) /home/mao1 cp: overwrite `/home/mao1/.bash_logout'? y cp: overwrite `/home/mao1/.bash_profile'? y cp: overwrite `/home/mao1/.bashrc'? y [root@local skel]# ls -a /home/mao1/ . .. .bash_logout .bash_profile .bashrc test1.dir .test.dir
也能成功,然后我又折騰了一下,到一個(gè)下一個(gè)用戶的家目錄,mao2(前面是mao1,請注意)。又執(zhí)行這條命令
[root@local ~]# cd /home/mao2/ [root@local mao2]# ls -A [root@local mao2]# cp -r $(ls -A /etc/skel/) /home/mao2/ cp: cannot stat `.bash_logout': No such file or directory cp: cannot stat `.bash_profile': No such file or directory cp: cannot stat `.bashrc': No such file or directory cp: cannot stat `test1.dir': No such file or directory cp: cannot stat `.test.dir': No such file or directory
居然給我反饋找不到文件和目錄,我瞬間懵逼了。why? 一模一樣的操作,居然前面能執(zhí)行就換個(gè)目錄就出錯(cuò)。瞬間感覺沒愛了。然后隔壁老王讓我排錯(cuò),天生作為一個(gè)trouble maker。走上一條不屬我的排錯(cuò)路,各種百度,google都無能給我解決。然后又將cp、ls的man文檔一一翻譯了一遍,均沒結(jié)果。 隨即想起“一支煙,解千愁,去萬恨?!惫?,回來的時(shí)候翻著ppt看,發(fā)現(xiàn)絕對路徑和相對路徑。仔細(xì)一看果然雖然我ls -A /etc/skel 顯示的是skel目錄下的文件,但是我cp的時(shí)候不在skel目錄下,系統(tǒng)就會在當(dāng)前路徑下尋找這幾個(gè)文件,恰巧當(dāng)前目錄沒有這幾個(gè),所以就出錯(cuò)了。各位看官,到此處應(yīng)該看出來,我的錯(cuò)誤了。接下來驗(yàn)證我的想法是否正確。
[root@localhost tmp]# ll -A total 0 [root@localhost tmp]# cd /etc/skel/ [root@localhost skel]# ls -A .bash_logout .bash_profile .bashrc .mozilla [root@localhost skel]# cp -r $(ls -A /etc/skel/) /tmp [root@localhost skel]# cd /tmp/ [root@localhost tmp]# ll -A total 12 -rw-r--r--. 1 root root 18 Aug 2 08:44 .bash_logout -rw-r--r--. 1 root root 193 Aug 2 08:44 .bash_profile -rw-r--r--. 1 root root 231 Aug 2 08:44 .bashrc drwxr-xr-x. 4 root root 37 Aug 2 08:44 .mozilla [root@localhost tmp]# cd /home/mao2 [root@localhost mao2]# ll -A total 0 [root@localhost mao2]# cd - /tmp [root@localhost tmp]# cp -r $(ls -A /etc/skel/) /home/mao2/ [root@localhost tmp]# cd - /home/mao2 [root@localhost mao2]# ls -A .bash_logout .bash_profile .bashrc .mozilla [root@localhost mao2]#
上面可以看出只要在有這幾個(gè)文件的目錄下能成功,那我們再去沒有這幾個(gè)文件的目錄下面執(zhí)行以下這條命令。
[root@localhost mao2]# cd /usr/tmp/ [root@localhost tmp]# ls -A [root@localhost tmp]# cp -r $(ls -A /etc/skel/) /home/mao2/ cp: cannot stat ‘.bash_logout’: No such file or directory cp: cannot stat ‘.bash_profile’: No such file or directory cp: cannot stat ‘.bashrc’: No such file or directory cp: cannot stat ‘.mozilla’: No such file or directory
誠不欺我,果然不能成功。作為一個(gè)小白繼續(xù)折騰,沒有路徑,我給你添加。
[root@localhost tmp]# cp -r /etc/skel/$(ls -A /etc/skel/) /home/mao2/ cp: overwrite ‘/home/mao2/.bash_logout’? y cp: cannot stat ‘.bash_profile’: No such file or directory cp: cannot stat ‘.bashrc’: No such file or directory cp: cannot stat ‘.mozilla’: No such file or directory
oh,shit。又只能拷貝一個(gè),一看原來中間有空格,系統(tǒng)把每個(gè)空格后面有當(dāng)成相對路徑。不怕,我們繼續(xù),反正作為一個(gè)trouble maker。我就是不斷創(chuàng)造麻煩的。那就繼續(xù),既然這樣我嘗試用xargs將結(jié)果輸出,然后再用sed在每個(gè)文件前面加個(gè)路徑。
[root@localhost tmp]# cp -r /etc/skel/{$(ls -A /etc/skel/ |xargs|sed -r "s#[[:space:]]#\,#g")} /tmp cp: cannot stat ‘/etc/skel/{.bash_logout,.bash_profile,.bashrc,.mozilla}’: No such file or directory [root@localhost tmp]# cp /etc/skel/{.bash_logout,.bash_profile,.bashrc,.mozilla} /usr/tmp/ cp: omitting directory ‘/etc/skel/.mozilla’ [root@localhost tmp]# cp -r /etc/skel/{.bash_logout,.bash_profile,.bashrc,.mozilla} /usr/tmp/ cp: overwrite ‘/usr/tmp/.bash_logout’? y cp: overwrite ‘/usr/tmp/.bash_profile’? y cp: overwrite ‘/usr/tmp/.bashrc’? y
這個(gè)問題的原因在于命令引用后的bash的{}特性不再被識別,/etc/skel/{.bashlogout,.bashprofile,.bashrc,.mozilla}被當(dāng)做一個(gè)整體的文件了。因此路不通。
[root@localhost ~]# ls -A /etc/skel/ |xargs|sed -r "s#(\..*[[:alpha:]]\b)#/etc/skel/\1#g" /etc/skel/.bash_logout .bash_profile .bashrc .mozilla
這個(gè)嘗試最為糾結(jié),嘗試了各種不同辦法,我在下面簡單寫一下部分嘗試。
[root@localhost tmp]# ls -A /etc/skel/ >1.txt [root@localhost tmp]# cat 1.txt .bash_logout .bash_profile .bashrc .mozilla [root@localhost tmp]# sed -r "s#(\..*[[:alpha:]]\>)#/etc/skel/\1#g" 1.txt /etc/skel/.bash_logout /etc/skel/.bash_profile /etc/skel/.bashrc /etc/skel/.mozilla
由此證明正則表達(dá)式?jīng)]錯(cuò)。go on;
oh,no, 又出錯(cuò)了。
[root@localhost tmp]# ls -A /etc/skel/ |xargs > 1.txt [root@localhost tmp]# sed -r "s#(\..*[[:alpha:]]\>)#/etc/skel/\1#g" 1.txt /etc/skel/.bash_logout .bash_profile .bashrc .mozilla
于是我懷疑是xargs命令的問題,我用加法運(yùn)算做了一個(gè)嘗試
[root@localhost tmp]# seq 1 10 |xargs|sed -r "s# #+#g" |bc 55 [root@localhost tmp]# seq 1 10|xargs 1 2 3 4 5 6 7 8 9 10 [root@localhost tmp]# seq 1 10 |xargs|sed -r "s# #+#g" 1+2+3+4+5+6+7+8+9+10 [root@localhost tmp]# seq 1 10 |xargs|sed -r "s# #+#g" |bc 55
xargs也沒有問題,同樣的命令,同樣的正則表達(dá)式居然就在上面行不通了。若各位看官能解決此問題,望通知小弟。
[root@localhost home]# for i in `ls -A /etc/skel/` ;do cp -r /etc/skel/$i /tmp ;done cp: overwrite ‘/tmp/.bash_logout’? y cp: overwrite ‘/tmp/.bash_profile’? y cp: overwrite ‘/tmp/.bashrc’? y
用for循環(huán)來實(shí)現(xiàn)這個(gè)功能,這個(gè)思路來自于teacher Rex。
cp -r /etc/skel/. /home/mao
隔壁老王的方法,這個(gè)方法是最完美,最簡便的方法。teacher wang 不虧是老司機(jī)。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
當(dāng)前題目:小白易患錯(cuò)誤之絕對路徑和相對路徑的操作錯(cuò)誤-創(chuàng)新互聯(lián)
URL地址:http://bm7419.com/article8/diopop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、靜態(tài)網(wǎng)站、搜索引擎優(yōu)化、動態(tài)網(wǎng)站、企業(yè)建站、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容