3 #From: kaz@ashi.footprints.net (Kaz Kylheku)
4 #Newsgroups: comp.os.linux.misc
5 #Subject: Re: bash question: subdirectories
6 #Message-ID: <slrn8a0gu9.v5n.kaz@ashi.FootPrints.net>
7 #Date: Tue, 08 Feb 2000 16:24:35 GMT
9 #Actually it can be made to. That is to say, it is possible to code a recursive
10 #descender function in the bash language. Here is an example.
12 #What is nice about this is that you can embed the function into your shell
13 #script. The function changes the current working directory as it descends.
14 #So it can handle arbitrarily deep paths. Whereas paths generated by the
15 #find command can cause a problem when they get too long; the kernel has a
16 #hard limit on the length of the string passed to the open() and other
19 #There are races; what if the directory tree is blown away during the traversal?
20 #The function won't be able to crawl back up using the .. link and will just
23 # Recursive Directory Traverser
28 # Function parameter usage:
29 # $1 directory to search
30 # $2 pattern to search for
31 # $3 command to execute
32 # $4 secret argument for passing down path
39 if [ "$4" = "" ] ; then
47 if [ -f "$file" ] ||
[ -d "$file" ]; then
52 if [ "$file" = "." ] ||
[ "$file" = ".." ] ; then
55 if [ -d "$file" ] && [ ! -L "$file" ]; then
56 recurse
"$file" "$2" "$3" "$path"
63 recurse
"$1" "$2" 'echo "$path$file"'