Loop over a set of files from the shell

Contributor Icon Contributed by qmchenry Date Icon October 8, 2003  
Tag Icon Tagged: Solaris

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 (say 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:

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.

Previous recipe | Next recipe |
 
  • guest
    Possibly a bit pedantic, but to save system time (especially if using multiple times in a script) you may be better to do the following for the last part...

    cat `ls -tr *.log` >biglog

    The cat is designed to be able to put two or more files together.

    By doing it this way, you don't need to worry about the existence or contents of biglog as you will overwrite it with the > rather than using apend to >>
blog comments powered by Disqus