x=$(find . -name "*.txt")
echo $x

如果我在Bash shell中运行上面的代码段,我得到的是一个包含几个由空白分隔的文件名的字符串,而不是一个列表。

当然,我可以进一步用空白分隔它们以得到一个列表,但我相信有更好的方法来做到这一点。

那么,循环查找命令结果的最佳方法是什么呢?


当前回答

(更新到包括@Socowi的卓越速度提升)

使用任何支持它的$SHELL (dash/zsh/bash…):

find . -name "*.txt" -exec $SHELL -c '
    for i in "$@" ; do
        echo "$i"
    done
' {} +

完成了。


原来的答案(更短,但更慢):

find . -name "*.txt" -exec $SHELL -c '
    echo "$0"
' {} \;

其他回答

查找<path> -xdev -type f -name *.txt -exec ls -l {} \;

这将列出文件并给出有关属性的详细信息。

# Doesn't handle whitespace
for x in `find . -name "*.txt" -print`; do
  process_one $x
done

or

# Handles whitespace and newlines
find . -name "*.txt" -print0 | xargs -0 -n 1 process_one

(更新到包括@Socowi的卓越速度提升)

使用任何支持它的$SHELL (dash/zsh/bash…):

find . -name "*.txt" -exec $SHELL -c '
    for i in "$@" ; do
        echo "$i"
    done
' {} +

完成了。


原来的答案(更短,但更慢):

find . -name "*.txt" -exec $SHELL -c '
    echo "$0"
' {} \;
find . -name "*.txt"|while read fname; do
  echo "$fname"
done

注意:此方法和bmargulies所示的(第二个)方法在文件/文件夹名称中使用空白时是安全的。

为了在文件/文件夹名称中包含换行符,你必须使用find的-exec谓词,就像这样:

find . -name '*.txt' -exec echo "{}" \;

{}是找到的项的占位符,\;用于终止-exec谓词。

为了完整起见,让我添加另一个变体-你必须喜欢*nix的多功能性:

find . -name '*.txt' -print0|xargs -0 -n 1 echo

这将使用\0字符分隔打印项,据我所知,在任何文件系统的文件或文件夹名称中都不允许使用\0字符,因此应该涵盖所有基础。Xargs一个接一个地把它们捡起来,然后……

我喜欢使用find,这是第一次分配给变量和IFS切换到新行如下:

FilesFound=$(find . -name "*.txt")

IFSbkp="$IFS"
IFS=$'\n'
counter=1;
for file in $FilesFound; do
    echo "${counter}: ${file}"
    let counter++;
done
IFS="$IFSbkp"

正如@Konrad Rudolph所评论的那样,这将不适用于文件名中的“新行”。我仍然认为它很方便,因为它涵盖了需要遍历命令输出的大多数情况。