-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
2
  • Quote the vars, also prefer find over bash Jan 26, 2016 at 22:08
  • -1 for repeatedly changing the question. This is looking like an XY problem.
    – muru
    Jan 26, 2016 at 22:39

1 Answer 1

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[@]}.

5
  • doesnt work so far, i need to put in an array, wait a sec to update question (your code works for echo and stat, but not array)
    – Then Enok
    Jan 26, 2016 at 22:11
  • @ThenEnok how are you putting it in an array?
    – muru
    Jan 26, 2016 at 22:12
  • @ThenEnok see update.
    – muru
    Jan 26, 2016 at 22:21
  • how can i edit for f in "${arrFiles[@]}" so that it give files in subfolders? :)
    – Then Enok
    Jan 26, 2016 at 22:36
  • @ThenEnok You don't. You edit arrfiles=( ".../"* ) to use recursive globs: unix.stackexchange.com/a/49917/70524
    – muru
    Jan 26, 2016 at 22:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .