3 回答
TA贡献1871条经验 获得超8个赞
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贡献1841条经验 获得超3个赞
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贡献1712条经验 获得超3个赞
for
# Don't do thisfor file in $(find . -name "*.txt")do …code using "$file"done
如果for循环甚至要启动,则 find
必须完成。 如果一个文件名中有任何空格(包括空格、制表符或换行符),那么它将被视为两个单独的名称。 尽管现在不太可能,但您可以溢出命令行缓冲区。假设您的命令行缓冲区包含32 KB,而您的 for
循环返回40 kb的文本。最后的8KB将从你的 for
循环你永远也不会知道。
while read
find . -name "*.txt" -print0 | while read -d $'\0' filedo …code using "$file"done
find
-print0
-d $'\0'
- 3 回答
- 0 关注
- 455 浏览
添加回答
举报