Bash List Files after a specific file
Question
I want to run my program on all the files in a directory after a specific file using a bash script.
If I have a directory like:
fileA
fileB
fileC
fileD
I want to run ./prog <file>
for all files after fileC
. How would I write a bash script to do this?
I currently have
for FILE in ./tests/*; do
./prog $FILE
if [ $? -eq 0 ]; then
echo "success: $FILE"
else
echo "**FAILURE: $FILE"
exit 1
fi
done
But, I want to start at a specific file in that directory. It doesn't necessarily need to be sorted since ls
list files in a specific order which is the same each time.
I typically just run my script, and when it fails, I fix it for that specific file, but then I'd want to resume from that file, and not restart from the beginning.
Solution
If the files are sorted, then you can use '<'
and '>'
operators to do a stringwise compare of two variables:
startfile=$1
for FILE in ./tests/*; do
if ! [ "$FILE" '<' "$startfile" ] ; then
echo doing something with $FILE
else
echo Skipping $FILE
fi
done
OTHER TIPS
for file in `ls tests | grep -A 999999 "^tests/startfile$" | tail -n +2`
do
whatever
done
grep -A 999999 lists tests/startfile (^ and $ to avoid partial match) and the 999999 next entries.
tail -n +2 lists everything starting from line 2 (1-based)