views:

43

answers:

3

I want to find a file with a certain name, but search in direcotories above the current one, instead of below.

I'd like something similar to: (except functional)

$ cd /some/long/path/to/my/dir/

$ find -maxdepth -1 -name 'foo'
/some/long/path/to/foo
/some/foo

Shell scripts or one-liners preferred.


In response to the several questions, the difference between the above example and the real find is that the search is proceeding upward from the current directory (and -maxdepth doesn't take a negative argument).

A: 

If you mean exclude the current dir:

find / -name 'foo' ! -iwholename "$PWD*"

If you mean: direct matches in any dir in the trail, this would work, but my bash-fu is not enough to easily get the list of dirs:

find  /some/ /some/long /some/long/path/ /some/long/path/to/ /some/long/path/to/my -maxdepth=1 -name='foo'

So all we need is a method to alter /some/long/path/to/my/dir to
/some/ /some/long /some/long/path/ /some/long/path/to/ /some/long/path/to/my

Wrikken
+2  A: 

You could use Parameter Expansion:

path="/some/long/path/to/my/dir"

while [ -n "$var" ]
do
  find $path -maxdepth 1 -name 'foo' 
  path="${var%/*}"
done
Helper Method
+2  A: 

This works, but it's not as simple as I hoped.

FILE=foo
DIR=$PWD
while [[ $DIR != '/' ]]; do
    if [[ -e $DIR/$FILE ]]; then
        echo $DIR/$FILE
    else
        DIR=`dirname $DIR`
    fi
done
bukzor
+!, this is the essential method
Joshua