How to loop through file names returned by find?

前端 未结 13 1435
野性不改
野性不改 2020-11-22 04:20
x=$(find . -name \"*.txt\")
echo $x

if I run the above piece of code in Bash shell, what I get is a string containing several file names separated

13条回答
  •  半阙折子戏
    2020-11-22 04:38

    (Updated to include @Socowi's execellent speed improvement)

    With any $SHELL that supports it (dash/zsh/bash...):

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

    Done.


    Original answer (shorter, but slower):

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

提交回复
热议问题