How to loop through file names returned by find?

前端 未结 13 1424
野性不改
野性不改 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:41

    I like to use find which is first assigned to variable and IFS switched to new line as follow:

    FilesFound=$(find . -name "*.txt")
    
    IFSbkp="$IFS"
    IFS=$'\n'
    counter=1;
    for file in $FilesFound; do
        echo "${counter}: ${file}"
        let counter++;
    done
    IFS="$IFSbkp"
    

    Just in case you would like to repeat more actions on the same set of DATA and find is very slow on your server (I/0 high utilization)

提交回复
热议问题