-1

I have the following code

arrFiles[0]=0
cd "/home/thenok/Documents/Link to thought diary"
for i in $(find -type f ! -name "*~"); do
arrFiles[0]=$[arrFiles[0]+1]
arrFiles[arrFiles[0]]="$i" 
echo "    pos ${arrFiles[0]} file ${arrFiles[arrFiles[0]]}"
done

but it outputs to (13 and 23 are files)

pos 354 file ./Link
pos 355 file to
pos 356 file diary/201504/quint/13
pos 357 file ./Link
pos 358 file to
pos 359 file diary/201511/23

which is bad because when using another command (stat) instead of echo it gives me

stat: cannot stat ‘./Link’: No such file or directory
stat: cannot stat ‘to’: No such file or directory
stat: cannot stat ‘thought’: No such file or directory
stat: cannot stat ‘diary/201411/done/31’: No such file or directory

I tried: 1: using a direct path instead of a link, 2:

arrFiles[0]=0
FILES="/home/thenok/Documents/Link to thought diary/*"
for i in "$FILES"; do
arrFiles[0]=$[arrFiles[0]+1]
arrFiles[arrFiles[0]]="$i" 
echo "    pos ${arrFiles[0]} file ${arrFiles[arrFiles[0]]}"
done
#result is pos 1 file /home/thenok/Documents/Link to thought diary/*

3: making the array directly doesn't use subfolders

arrFiles=( "/home/thenok/Documents/Link to thought diary/"* )
for f in "${arrFiles[@]}"
do
stat -c %s "$f"
echo "$f"
done

Solution found at How to read complete line in 'for' loop with spaces which is exactly what i wanted in the first place but didnt know how to write in google

IFS=$'\n'       # make newlines the only separator
arrFiles[0]=0
cd "/home/thenok/Documents/Link to thought diary"
for i in $(find "/home/thenok/Documents/Link to thought diary/" -type f ! -name "*~"); do
arrFiles[0]=$[arrFiles[0]+1]
arrFiles[arrFiles[0]]="$i" 
echo "    pos ${arrFiles[0]} file ${arrFiles[arrFiles[0]]}"
done

output is

pos 10 file /home/thenok/Documents/Link to thought diary/Link to diary/201408/methinks questions.txt
pos 11 file /home/thenok/Documents/Link to thought diary/Link to diary/201408/methinks small.txt
pos 12 file /home/thenok/Documents/Link to thought diary/Link to diary/201408/methinks2.txt
pos 13 file /home/thenok/Documents/Link to thought diary/Link to diary/201408/methinkss.txt
pos 14 file /home/thenok/Documents/Link to thought diary/Link to diary/201408/myfile.txt
pos 15 file /home/thenok/Documents/Link to thought diary/Link to diary/201408/new  2.txt
Then Enok
  • 815

1 Answers1

3

Why does my shell script choke on whitespace or other special characters?

Do it thus:

FILES="/home/thenok/Documents/Link to thought diary/"
for f in "$FILES"/*
do
    stat "$f"
done

If you use find, use its -exec command:

find "/home/thenok/Documents/Link to thought diary/" -type f ! -name '*~' -exec stat {} +

Never do for f in $(find ...).

If you want an array, just create one directly:

arrFiles=( "/home/thenok/Documents/Link to thought diary/"* )

Then do:

for f in "${arrFiles[@]}"
do
    stat -c %s "$f"
done

Instead of looping over indices. Even then, you don't need to save the length of an array manually. You can get it through ${#arrFiles[@]}.

muru
  • 197,895
  • 55
  • 485
  • 740