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
> mkdir $file.dir
> ( cd $file.dir; gzip -dc $file | tar xf - )
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`
> cat $x >> biglog
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.