www.LinuxHowtos.org


Combining Commands with For

This tip shows you how to run similar commands in a loop to avoid typing in the same command over and over again. For example, untarring several tar.gz files. Or perhaps renaming files with similar extensions.

Code Listing 1: for and tar

# for n in *.tar.gz; do tar -zxvf $n; done

This next instance demonstrates removing the .dist extension of several files.

Code Listing 2: for and mv

# for n in *.dist; do mv $n `basename $n .dist`; done

If necessary, you could combine it with find to rename all .phtml files in /home/httpd/htdocs to .php

Code Listing 3: for and find

  # cd /home/httpd/htdocs 
  # for n in `find -type f -name '*.phtml'`;   
      do mv $n `basename $n .phtml`.php; done

From http://www.gentoo.org/news/en/gwn/20030609-newsletter.xml

back