theholy
theholy

Reputation: 409

Linux Script, for on files of a directory

I need to do a shell script that lauch some parameter above a files in a directory.

I am using something like

for f in *.tmp
do
        echo  $f
        sleep 5
done

My problem is that during the execution of the for, the number of files in the directory may change. And the for only applies in the files listed at the time of first execution.

Any Ideas ?? Thanks a lot!

Upvotes: 0

Views: 19

Answers (1)

glenn jackman
glenn jackman

Reputation: 247012

You can store the files in an array

files=(*.tmp)

Then use an infinite while loop, and break out when a condition is met:

i=0
while true; do
    do_something with "${files[i]}"
    current_files=(*.tmp)
    magic_handwaving -- some code to compare "files" with "current_files" \
        that will append new files to "files"
    (( ++i == ${#files[@]} )) && break
done
echo all files processed

Upvotes: 1

Related Questions