2017-01-02 41 views
-1

我試圖將「file.txt」文件複製到所有目錄中。爲什麼我在cp命令中使用globbing獲得「omitting directory」錯誤?

[[email protected]]# ls -lh 
total 132K 
drwxr--r--. 2 postgres postgres 4.0K Jan 2 08:47 ilo01 
drwxr--r--. 2 postgres postgres 4.0K Jan 2 08:40 ilo02 
drwxr--r--. 2 postgres postgres 4.0K Jan 2 08:40 ilo03 
drwxr--r--. 2 postgres postgres 4.0K Jan 2 08:40 ilo04 
drwxr--r--. 2 postgres postgres 4.0K Jan 2 08:40 ilo05 
drwxr--r--. 2 postgres postgres 4.0K Jan 2 08:40 ilo06 
drwxr--r--. 2 postgres postgres 4.0K Jan 2 08:40 ilo07 
drwxr--r--. 2 postgres postgres 4.0K Jan 2 08:40 ilo08 
drwxr--r--. 2 postgres postgres 4.0K Jan 2 10:03 ilo09 
drwxr--r--. 11 postgres postgres 4.0K Jan 2 11:15 ilo10 
-rw-r--r--. 1 postgres postgres 64K Jun 27 2016 file.txt 

使用TAB鍵才能看到該命令的行爲來運行:

[[email protected]]# cp -p file.txt ilo[0-1][0-9] 
ilo01/ ilo02/ ilo03/ ilo04/ ilo05/ ilo06/ ilo07/ ilo08/ ilo09/ ilo10/ 

但我得到這個錯誤:

[[email protected]]# cp -v -p file.txt ilo[0-1][0-9]* 
`postgresTdas.txt' -> `ilo10/file.txt' 
cp: omitting directory `ilo01' 
cp: omitting directory `ilo02' 
cp: omitting directory `ilo03' 
cp: omitting directory `ilo04' 
cp: omitting directory `ilo05' 
cp: omitting directory `ilo06' 
cp: omitting directory `ilo07' 
cp: omitting directory `ilo08' 
cp: omitting directory `ilo09' 

同樣的事情發生的:

[[email protected]]# cp -p file.txt ilo* 

[[email protected]]# cp -p file.txt ilo*/ 

我不明白爲什麼「[0-1] [0-9]不按我需要的方式工作。

我假設副本將把tab.txt放在TAB顯示的列表中。

我錯過了什麼?

+2

'cp'可以將一個或多個文件複製到單個目標目錄,或者它可以將文件複製到單個目標文件。它不能將單個文件複製到多個目標。您需要創建一個循環將您的文件複製到所有這些目錄。 – codeforester

+4

你可以在這裏找到你的答案:http://stackoverflow.com/questions/195655/how-to-copy-a-file-to-multiple-directories-using-the-gnu-cp-command – codeforester

+0

謝謝。我對這個問題感到非常慚愧:/ – manuelc

回答

2

cp命令的大多數實現都能夠複製到多個目標。你無能爲力。您需要解決多次調用cp的限制。最簡單的可能是這樣的:

ls -d dir* | xargs -n 1 cp file.txt 
1

的參數擴展到文件+所有目錄

cp考慮的最後一個參數爲目標,因此其他目錄被認爲是來源。

而且因爲除非-r-R選項設置(複製目錄內容)cp不會複製目錄,你會得到所有的目錄,但最後一個警告。

我會做到這一點與一個bash/sh的腳本:

for d in ilo[0-1][0-9] 
do 
    cp -p file.txt $d 
done 
+0

我是上帝,我做了一個bubu。所有的答案都是正確的。我把它交給阿卡斯卡接受,因爲他先回應了。但是非常感謝你。 – manuelc

+0

46分鐘前對陣55分鐘前?我首先回應。但如你所願。你是老闆:) –

相關問題