Tag Archives: shell

Bash Shell Script to Loop Over Set of Files

I’m often asked how to write a shell script that loops over a set of files in the current directory and perform some actions on them. Really, nothing is simpler than that, it’s just that most administrators or developers don’t take the time to get into shell scripting.

The simplest form is this, simply change the wildcard characters to match your requirements:

#!/bin/bash
FILES="*.txt"
for f in $FILES
do
  echo "Processing $f ..."
  # perform some action on each file. $f stores the current file name
  echo "Done processing $f ..."
done