HomeUNIXSolarisLoop Over a Set of Files from the Shell

Loop Over a Set of Files from the Shell

Shell scripts are powerful because anything that can be done from the shell can be done in a shell script. Likewise, functionality seen usually only in shell scripts like looping and conditionals is fair game, typed directly into the shell. Looping over a set of files using a for loop is a simple example.


If there are several gzip archives in a directory and you want to extract them into separate directories, looping from the shell is one solution. For example, file1.tar.gzip and file2.tgz exist and are to be extracted into directories file1.tar.gzip.dir and file2.tgz.dir, respectively. If using a Bourne-type shell (sh, ksh, zsh, bash), use the following commands from the command line. (After you press enter at the end of each line, the shell will change the prompt to > to let you know it is still expecting more.):

for file in *gz
> do
> mkdir $file.dir
> ( cd $file.dir; gzip -dc $file | tar xf - )
> done

Enclosing the cd and gzip/tar commands in parentheses makes the directory change affect only those gzip/tar commands, not subsequent ones.

Another example shows how to create a file that consists of the contents of a number of files (such as log files) sorted by their last modification date. To create a file biglog made out of all files in the current directory sorted by date, use the following:

for x in `ls -tr *.log`
> do
> cat $x >> biglog
> done

The command ls -tr *.log lists all files ending in .log in chronological order. This command is enclosed in `special quotes`, not the ‘usual single quotes’ which causes the command to be executed and its output returned to the shell for use like a variable. The biglog file needs to be empty or deleted before these commands are run.

Quinn McHenry
Quinn McHenry
Quinn was one of the original co-founders of Tech-Recipes. He is currently crafting iOS applications as a senior developer at Small Planet Digital in Brooklyn, New York.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

LATEST REVIEWS

Recent Comments

error: Content is protected !!