Search code examples
bashshellls

Bash List Files after a specific file


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