| Line | |
|---|
| 1 | #! /bin/bash
|
|---|
| 2 | #From: [email protected] (Kaz Kylheku)
|
|---|
| 3 | #Newsgroups: comp.unix.shell
|
|---|
| 4 | #Subject: Why not roll your own @#$% find! (was: splitting directory off from filename)
|
|---|
| 5 | #Message-ID: <[email protected]>
|
|---|
| 6 | #Date: Fri, 26 Jun 1998 20:47:34 GMT
|
|---|
| 7 |
|
|---|
| 8 | # $1 = dirname, $2 = pattern, optional $3 = action
|
|---|
| 9 | xfind()
|
|---|
| 10 | {
|
|---|
| 11 | local x
|
|---|
| 12 | local dir="$1"
|
|---|
| 13 |
|
|---|
| 14 | # descend into specified directory
|
|---|
| 15 |
|
|---|
| 16 | builtin cd -L "$1" || {
|
|---|
| 17 | echo "${FUNCNAME}: cannot change dir to $1" >&2
|
|---|
| 18 | return 1
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | #
|
|---|
| 22 | # default action is to print the filename
|
|---|
| 23 | #
|
|---|
| 24 | if [ -n "$3" ]; then
|
|---|
| 25 | action="$3"
|
|---|
| 26 | else
|
|---|
| 27 | action='printf -- "%s\n"'
|
|---|
| 28 | fi
|
|---|
| 29 |
|
|---|
| 30 | # process ordinary files that match pattern
|
|---|
| 31 |
|
|---|
| 32 | for x in $2 ; do
|
|---|
| 33 | if [ -f "$x" ] ; then
|
|---|
| 34 | eval "$action" "$x"
|
|---|
| 35 | fi
|
|---|
| 36 | done
|
|---|
| 37 |
|
|---|
| 38 | # now descend into subdirectories, avoiding symbolic links
|
|---|
| 39 | # and directories that start with a period.
|
|---|
| 40 |
|
|---|
| 41 | for x in * ; do
|
|---|
| 42 | if [ -d "$x" ] && [ ! -L "$x" ] ; then
|
|---|
| 43 | $FUNCNAME "$x" "$2" "$action"
|
|---|
| 44 | fi
|
|---|
| 45 | done
|
|---|
| 46 |
|
|---|
| 47 | # finally, pop back up
|
|---|
| 48 |
|
|---|
| 49 | builtin cd -L ..
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | #xfind "$@"
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.