Spaces in the path names give problems with Find in Bash. Any * simple * workflow?

Is there a way to change the next line so that I don't have problems when they have files / folders with spaces in them?

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

I would prefer that there be a solution that would not require changing other parts of my code, but this line of code, as everything seems to work correctly, except for this minor detail.

thank

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

abc=( $(find "$pasta" -name "$ficheiro_original@*" -type f) )
abc_length=${#abc[@]}
+3
source share
5 answers

If you do not use these file names later in your script, just iterate over them and process them 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
+9
source

, , , . - :

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

script, , , ( ) , . , , $files, "${files[@]}"

ls "${files[@]}"
for f in "${files[@]}"; do
    ls "$f"
done
echo "found ${#files[@]} files" 
+2

, , GNU Find -print0 ..

find ~/$folder -name "*@*" -type f -print0 |
while read -d '^@' file
do
    echo "<<$file>>"
done

( , "^@", ASCII NUL ('\ 0'; Control-V Control-Shift-@).

find ~/$folder -name "*@*" -type f -print0 |
while read -d '' file
do
    echo "<<$file>>"
done

" , ASCII NUL " "find ... -print0". ( .)

. , bash , script.

( , , , read , , , .)

+2

, :

# files=($(find))
eval "files=($(find -printf '"%h/%f" '))"

for f in "${files[@]}"; do
  echo "$f"
done

, . ". eval Bash -printf of find .

$IFS, FYI.

+1

Bash, "read":

printf '%q\n' "$IFS"

IFS=$'\n' read -r -d "" -a abc <<< "$(find ~/$folder -name "*@*" -type f)"
IFS=$'\n' read -r -d "" -a abc < <(find ~/$folder -name "*@*" -type f)  # alternative

abc_length=${#abc[@]}

for ((i=1; i <= ${#abc[@]}; i++)); do echo "$i:  ${abc[i-1]}"; done

printf '%q\n' "$IFS"

, IFS ( IFS ).

+1

Source: https://habr.com/ru/post/1768805/


All Articles