Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

To find files starting from a certain path, I can use find <path> .... If I want to find 'upwards', i.e. in the parent directory, and it's parent, and..., is there an equivalent tool?

Intended usage for a folder structure like this:

/
/abc
/abc/dce/efg/ghi


$ cd /abc/dce/efg/ghi
$ touch ../../x.txt
$ upfind . -name X*
../../x.txt
$ upfind . -name Y*
$
share|improve this question
    
Can you explain what you are trying to achieve with this ? –  Iain Jul 30 '12 at 10:02
    
Surely: I have a makefile that needs to know the path to a common piece of code implemented in a makefile somewhere up the chain. –  xtofl Jul 30 '12 at 10:47
    
@Iain: ... and I need to know the number of dots I have to type in to reach it. A common problem, I assumed. –  xtofl Jul 30 '12 at 12:03

4 Answers 4

If you want the parent directory of the file you can use find and the -printf %h argument

find /abc -name X.txt -printf "%h\n"
/abc/dce

  • %h

    Leading directories of file’s name (all but the last ele- ment). If the file name contains no slashes (since it is in the current directory) the %h specifier expands to ".".

share|improve this answer
x="$(pwd)"; while [ "$x" != "/" ]; do if [ -e "${x}/X.txt" ]; then echo $x; fi; x="$(dirname "$x")"; done
share|improve this answer
    
You missed an fi; –  Iain Jul 30 '12 at 10:29
    
I surerly like the attempt, but I think it needs an else, too (x is only changed if the file is found). And the part where the path is expressed relatively to the current working dir isn't there. –  xtofl Jul 30 '12 at 10:44

Why not just do a downward-recursive find from /? They will both search the entire filespace. Or are you asking for one that travels up but then does not travel down in the directories it finds in that search?

share|improve this answer
    
Indeed: the latter. Actually I want to find the parent dir that contains X.txt. –  xtofl Jul 30 '12 at 9:32
    
@xtofl cd ..;find blah? –  pauska Jul 30 '12 at 10:41
    
@pauska: no, since then I could find blah in a sibling directory, too. –  xtofl Jul 30 '12 at 12:11

Based on the Idea of @Womble, I wrote a small script that does the trick:

#!/bin/bash
#
# rfind: finds a file in one of the parent directories

needle=$1 
current_dir=$(pwd) 
path=

while [ "$current_dir" != "$(dirname $current_dir)" ]; do 
    if [ -e "${current_dir}/$needle" ]; then 
        echo $path$needle
        exit 0
    else 
        path=../$path
        current_dir="$(dirname "$current_dir")"
    fi
done

if [ ! "$current_dir" != "$(dirname $current_dir)" ]; then
    echo "rfind: file $needle not found" >&2
    exit 1
fi

And named it 'rfind' in my own ~/bin directory. It does the trick:

/tmp $ mkdir -p x/y/z
/tmp $ cd x/y/z/
/tmp/x/y/z $ rfind a.txt || echo "not found."
rfind: file a.txt not found
not found.
/tmp/x/y/z $ touch ../../a.txt
/tmp/x/y/z $ rfind a.txt || echo "not found."
../../a.txt
/tmp/x/y/z $ 
/tmp/x/y/z $ cd ..
/tmp/x/y $ mkdir z2
/tmp/x/y $ cd z
/tmp/x/y/z $ touch ../z2/a.txt
/tmp/x/y/z $ rfind a.txt || echo "not found."
../../a.txt
/tmp/x/y/z $ rm ../../a.txt
/tmp/x/y/z $ rfind a.txt || echo "not found."
rfind: file a.txt not found
not found.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.