3 回答

TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超8個(gè)贊
find . -name '*.txt' -exec process {} \;
for i in $x; do # Not recommended, will break on whitespace process "$i"done
x
:
for i in $(find -name \*.txt); do # Not recommended, will break on whitespace process "$i"done
for i in *.txt; do # Whitespace-safe but not recursive. process "$i"done
globstar
# Make sure globstar is enabledshopt -s globstarfor i in **/*.txt; do # Whitespace-safe and recursive process "$i"done
read
:
# IFS= makes sure it doesn't trim leading and trailing whitespace# -r prevents interpretation of \ escapes.while IFS= read -r line; do # Whitespace-safe EXCEPT newlines process "$line"done < filename
read
find
find . -name '*.txt' -print0 | while IFS= read -r -d '' line; do process $line done
find
-exec
-print0 | xargs -0
:
# execute `process` once for each filefind . -name \*.txt -exec process {} \;# execute `process` once with all the files as arguments*:find . -name \*.txt -exec process {} +# using xargs*find . -name \*.txt -print0 | xargs -0 process# using xargs with arguments after each filename (implies one run per filename)find . -name \*.txt -print0 | xargs -0 -I{} process {} argument
find
-execdir
-exec
-ok
-exec
-okdir
-execdir
).
find
xargs

TA貢獻(xiàn)1841條經(jīng)驗(yàn) 獲得超3個(gè)贊
find . -name "*.txt"|while read fname; do echo "$fname"done
-exec
find
find . -name '*.txt' -exec echo "{}" \;
{}
\;
-exec
find . -name '*.txt' -print0|xargs -0 -n 1 echo
\0
xargs

TA貢獻(xiàn)1712條經(jīng)驗(yàn) 獲得超3個(gè)贊
for
# Don't do thisfor file in $(find . -name "*.txt")do …code using "$file"done
如果for循環(huán)甚至要啟動(dòng),則 find
必須完成。 如果一個(gè)文件名中有任何空格(包括空格、制表符或換行符),那么它將被視為兩個(gè)單獨(dú)的名稱。 盡管現(xiàn)在不太可能,但您可以溢出命令行緩沖區(qū)。假設(shè)您的命令行緩沖區(qū)包含32 KB,而您的 for
循環(huán)返回40 kb的文本。最后的8KB將從你的 for
循環(huán)你永遠(yuǎn)也不會(huì)知道。
while read
find . -name "*.txt" -print0 | while read -d $'\0' filedo …code using "$file"done
find
-print0
-d $'\0'
添加回答
舉報(bào)