title: Bash: search up a directory tree
body: <p>I have a source code tree whose root is at something like <code>/home/me/workspace</code>.  There are many subdirectories many levels deep.  In particular there is a path containing some tools:</p>
  
  <pre><code>/home/me/workspace/tools/scripts
  </code></pre>
  
  <p>I am writing a bash function which I can call from any place in the tree to which I pass the string <code>tools/scripts</code>.  The function should iterate its way from the present working directory to <code>/</code> looking for path fragment <code>tools/scripts</code>, then if it finds it, print out the absolute path in which it is found.  In this example, <code>/home/me/workspace</code> would be printed.  If the path fragment is not found at all, then nothing is printed.</p>
  
  <p>I already have the following bash function which does this for me:</p>
  
  <pre><code>search_up ()
  (
      while [ $PWD != "/" ]; do
          if [ -e "$1" ]; then
              pwd
              break
          fi
          cd ..
      done
  )
  </code></pre>
  
  <p>but this seems a bit long-winded.  I am wondering if there are any other ways to do this either in bash itself, or in perhaps a single <code>find</code> command, or any other common utility.  I'm particularly looking for readability and brevity.</p>
  
  <p>Note I am <strong>not</strong> looking for a full recursive search of the entire tree.</p>
  
  <p>Also my bash is not the latest, so please no tricks using the latest, greatest:</p>
  
  <pre><code>$ bash --version
  GNU bash, version 3.00.15(1)-release (x86_64-redhat-linux-gnu)
  Copyright (C) 2004 Free Software Foundation, Inc.
  $
  </code></pre>