bashfind

Spaces in path names giving trouble with Find in Bash. Any *simple* work-around?


Is there any way to change the following string so I don't get any problems when there are files/folders with spaces in them?

files=`find ~/$folder -name "*@*" -type f`

I'd prefer if there was a solution that wouldn't have to involve having to change other parts of my code but this line of code, as everything seems to be working correctly, apart from this minor detail.

Thanks

EDIT: Here is the code in a bit more detail:

abc=( $(find "$pasta" -name "$ficheiro_original@*" -type f) )
abc_length=${#abc[@]}

Solution

  • If you are not using those file names later in your script , just iterate them and process on the fly.

    find ~/$folder -name "*@*" -type f | while read -r FILE
    do
      echo "do you stuff"
    done
    

    otherwise, you can set IFS

    IFS=$'\n'
    files=$(find ~/$folder -name "*@*" -type f)
    

    Update:

    $ IFS=$'\n'
    $ a=($(find . -type f ))
    $ echo ${#a[@]}
    14