有3個txt檔案,分別叫1.txt 2.txt 3.txt
我想使用通配符批量復制名稱為 1.txt.cp 2.txt.cp 3.txt.cp *
我輸入了命令 cp *.txt *.txt.cp 但它不起作用......
cp : target *.txt.cp : 不是目錄是什么問題???
uj5u.com熱心網友回復:
用: for i in *.txt; do cp "$i" "$i.cp"; done
例子:
$ ls -l *.txt
-rw-r--r-- 1 halley halley 20 out 27 08:14 1.txt
-rw-r--r-- 1 halley halley 25 out 27 08:14 2.txt
-rw-r--r-- 1 halley halley 33 out 27 08:15 3.txt
$ ls -l *.cp
ls: could not access '*.cp': File or directory does not exist
$ for i in *.txt; do cp "$i" "$i.cp"; done
$ ls -l *.cp
-rw-r--r-- 1 halley halley 20 out 27 08:32 1.txt.cp
-rw-r--r-- 1 halley halley 25 out 27 08:32 2.txt.cp
-rw-r--r-- 1 halley halley 33 out 27 08:32 3.txt.cp
$ for i in *.txt; do diff "$i" "$i.cp"; done
$
uj5u.com熱心網友回復:
如果您習慣了 MS/Windown CMD shell,請務必注意 Unix 系統對通配符的處理方式非常不同。MS/Windows 保留了 MS/DOS 規則,即不解釋通配符而是傳遞給命令。該命令會看到通配符,并且可以處理*命令中的第二個字符,以記錄第一個匹配項的位置,從而使之變得copy ab.* cd.*合理。
在 Unix(以及 Linux 等衍生產品)中,shell 負責處理通配符,并用所有可能的匹配項替換包含 1 的任何單詞。好訊息是該命令不必關心這個。但缺點是如果當前檔案夾包含 ab.txt ab.md5 cd.jpg,一個命令copy ab.* cd.*將被翻譯成copy ab.txt ab.md5 cd.jpg你可能不想要的......
根本原因是 Unix shell 比從 MS/DOS 繼承而來的 CMD.EXE 更通用,并且確實具有簡單易用for和if復合命令。只需查看@Halley Oliveira對您用例的語法的回答即可。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/338741.html
下一篇:格式化腳本輸出
