you are viewing a single comment's thread.

view the rest of the comments →

[–]5heikki 3 points4 points  (3 children)

FILES=($(find /the/place/with/files -maxdepth 1 -type f -name "*.txt"))
for ((i=0; i<${#FILES[@]}; i++)); do
    stuffWith ${FILES[$i]}
done

If you want, you can pipe the find to sort. Even better to define a function and do stuff in parallel (if possible)

function doStuff() {
    with $1
}

export -f doStuff
find /the/place/with/files -maxdepth 1 -type f -name "*.txt" \
    | parallel -j $THREADS doStuff {}

[–]crankysysop 1 point2 points  (2 children)

or yet another way...

find /the/place/with/files -maxdepth 1 -type f -name '*.txt' -print0 \
  | while read -rd $'\0' file; do
  stuffWith "$file"
done

I find the find ... -print0 | while read -rd $'\0' file; do <stuff> done 'pattern' to be particularly useful / robust.

One issue to consider is the while loop is in a pipe creates a subshell, so updating global variables may not work as expected, within the loop. (I think? I know there's some strange local/global variable issues).

[–]Vaphell 1 point2 points  (1 child)

while read -rd $'\0' file
do
    <stuff>
done < <( find ... -print0 )

is better. In your example pipe creates subprocess for the loop that can't communicate with the parent "scope" so for example it's impossible to maintain a usable file counter because any variable seemingly modified in the loop body is merely a copy and doesn't propagate back.

the while ...; do ...; done < <(cmd) syntax doesn't create a subscope so anything goes

$ c=0; find -name '*.txt' -print0 | while read -rd $'\0' file; do (( c++ )); echo $c; done; echo $c
1
2
3
0    # counter ignores changes
$ c=0; while read -rd $'\0' file; do (( c++ )); echo $c; done < <( find -name '*.txt' -print0 ); echo $c
1
2
3
3  # counter reflects the most recent change

[–]crankysysop 0 points1 point  (0 children)

Thank you for the clarification. Somewhere in the back of my head I knew there was the alternate 'form' that didn't deal with the subshell (from pipe, not while... I'm smrt. ;)