Iterating over a directory's contents with BASH

If you need to perform an operation on every file in a directory, you can use a BASH's for loop as directory contents will automatically read a without the need to grab the contents first via another system command.

The following BASH script will print out the contents of the /home directory:

DIR=/home
for file in ${DIR}/*
do
   echo "Found ${file}"
done

Last updated: 18/03/2012