我發現自己經常做以下幾點:如何執行shell命令「for」來命名帶空格的文件名?
for f in `find -foo -bar -baz`; do
process "$f"
done
當然這並不適用於帶有空格的文件名使用。我該如何處理這種情況?
我發現自己經常做以下幾點:如何執行shell命令「for」來命名帶空格的文件名?
for f in `find -foo -bar -baz`; do
process "$f"
done
當然這並不適用於帶有空格的文件名使用。我該如何處理這種情況?
find . -type f | while read file; do
process "$f"
done;
在這種情況下,我的做法是for
命令之前建立列表和其他字符或字符串這是不可能出現替換元素名稱中的空白。
然後在循環中,我用空白替換回特定的字符串。
一個例子:
list=`find -foo -bar -baz | tr ' ' 'µ'`
for fx in $list ; do
f=`echo $fx | tr 'µ' ' '`
process "$f"
done
這是冗長而不健壯的。 – 2010-02-17 09:29:45
如果使用的是發現已經,爲什麼不直接使用EXEC
find -foo -bar -baz -exec process '{}' \;
的另一種解決辦法是改變IFS變量(除現場分隔符)
尋找和xargs一起工作。找到可以打印文件的名稱與\0
-delimiter(選件print0
)和xargs的可以在格式閱讀(選件-0
):
find . -type f -print0 | xargs -0 echo
不要忘記xargs!在大量文件列表中,您可能會發現自己遇到了「太多爭論」錯誤。這兩篇O'Reilly文章解釋了這種情況:http://linuxdevcenter.com/pub/a/linux/lpt/09_21.html http://linuxdevcenter.com/pub/a/linux/lpt/09_22.html – Jim 2010-02-16 21:49:14
慶典4
shopt -s globstar
for file in /path/**
do
process "$file"
done
這是* *完全相同我需要什麼。謝謝! – AdSR 2010-02-16 13:01:03
「$ f」應該是「$ file」。使用'while read -r'來獲取文件名中的文字反斜槓 – ghostdog74 2010-02-16 15:06:20