Removing a.out’s with a shell script

Sometimes you end up with a bunch of code compiled on a system, and a bunch of a.out files, created by compilers like C, and Fortran. In reality it seems like a nice idea to give programs better names than a.out, but there is a huge potential benefit. It is extremely easy to purge a system of hidden a.out files in a multitude of directories. Especially if you know something about recursion. Shell scripts can use it as well. Below is a Bourne shell script to eliminate a.out files from the current directory downwards in all subsidiary directories.

#!/bin/sh

recursiverm() {
   for d in *; do
      if [ -d "$d" ]; then
         (cd "$d"; recursiverm)
      fi
      if [ "$d" == "a.out" ]; then
         rm $d
         echo "$d"
      fi
   done
}

(cd .; recursiverm)

The code basically look in the current directory. If a file is a sub-directory, recursively process it. If it is a file equal to “a.out”, delete it. If it hits the end of a directory, it terminates.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.