Thursday, July 29, 2010 22:23

Bash Shell Loop Over Set of Files

Tagged with:
Posted by ukoom on Monday, August 17, 2009, 9:06
This news item was posted in Linux category and has 0 Comments so far.

Q. How do I run shell loop over set of files stored in a current directory or specified directory?

A. You can use for loop easily over a set of shell file under bash or any other UNIX shell using wild card character.

Using a shell variable and for loop

You can use shell variable to store all file names. For example, store all *.c file in a variable called FILES:

$ FILES="*.c"
Now to loop through all files, enter:
$ for f in "$FILES"; do echo "Processing $f file.."; done

Sample shell script to loop through all files

#!/bin/bash
FILES="*"
for f in "$FILES"
do
  echo "Processing $f file..."
  # take action on each file. $f store current file name
  cat $f
done
This article is from http://www.cyberciti.biz/faq/bash-loop-over-file

Related Posts

Leave a Reply

You can leave a response, or trackback from your own site.