#! /bin/bash # # Filename: findall # # Programmer: Br. David Carlson # # Date: March 4, 2003 # # This script does a recursive listing of all files in the directory tree rooted at the # first command-line parameter. Output is to standard out and gives the full pathname of # each file. # # Note that this script was created as an example of how to write a recursive function in a # script. A more practical way to get a recursive listing of files is simply to use the # ls command with the appropriate option as in: # ls -lR # However, that command will not give full pathnames. If you really need full pathnames you # could use a find command, such as: # find # However, the find command given will not follow a symbolic link to a directory, whereas findall # will. The find command will find and list files and directories that start with a . whereas # the findall command will not. # # Note that this script assumes that it has access to all directories in the above-mentioned # directory tree. If this is not true, you will get error meessages and be left with # temporary files in /tmp. # # Syntax: # findall StartDir # The following function processes one directory. The sole input parameter is: # 1 the name of the directory to process # This recursive function lists the complete pathname of all files in the directory tree # rooted at the directory given as the first parameter. Each new call of this function # also adds 1 to the global variable count, which is used as part of the name of each new # temp file. onedir() { # Local variables for this function: local TMP local OLDDIR local DIR local fname # bash syntax for adding 1 to a variable: count=$[$count + 1] TMP=/tmp/findall.$$.$count OLDDIR="`pwd`" cd "$1" DIR="`pwd`" ls > $TMP sync while read fname do echo "${DIR}/$fname" if [ -d "$fname" ] then # Recursive call of the function: onedir "$fname" fi done < $TMP rm $TMP cd "$OLDDIR" } # Start of main portion of the script: if [ $# -ne 1 ] then echo "Error: one parameter is needed" echo "Syntax: findall StartDir" exit 1 fi count=1 onedir "$1" exit 0