views:

125

answers:

3

Hi, I have written (tried to) this small bash script for searching through a range of directories.

#!/bin/bash
shopt -s nullglob
for file in [ac]*/blarg
do 
   echo $file
   done

This script searches through directories starting with "a" through "c" for "blarg". It only goes one level deep. How can I make it step through all directories it might encounter, and not just the root of the directories with the starting letter.

Also, is this question supposed to go here at stackoverflow or would superuser be more suitable?

Thanks

+2  A: 

on the command line ths will do your purpose.so why to go for a script?

find ./[ac]*/ -name "blarg"

if you still need a script:

#!/bin/bash
shopt -s nullglobi
for file in `find ./[ac]*/ -name "blarg"`
do
echo $file
done
Vijay Sarathi
+1 better answer than mine.
Richard Pennington
It should be `./[ac]*/`. It also fails if filenames have spaces. It should be `find... | while read file...` or `while read file...done < <(find ...)`
Dennis Williamson
Closer, but using `read` will still fail with pathnames with newlines in them. `find [ac]*/ -name blarg -print0 | xargs -0 bash -c 'for file in "$@"; do …; done'`
Chris Johnsen
@Chris/Dennnis: `while IFS= read -r -d $'\0' file; do...done < <(find ... -print0)` That will handle any file name you can throw at it. No need for xargs.
SiegeX
A: 
echo `find blarg`

Replace that line and you'll find all files under [ac]* named blarg.

Richard Pennington
+3  A: 

if you have Bash 4.0 you can try globstar

#!/bin/bash
shopt -s nullglob
shopt -s globstar
for file in [ac]*/**/blarg
do 
   echo $file
done