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:

for file in ${DIR}/*
   echo "Found ${file}"

Last updated: 18/03/2012