How to loop through file names returned by find?

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

    You can put the filenames returned by find into an array like this:

    array=()
    while IFS=  read -r -d ''; do
        array+=("$REPLY")
    done < <(find . -name '*.txt' -print0)
    

    Now you can just loop through the array to access individual items and do whatever you want with them.

    Note: It's white space safe.

提交回复
热议问题