Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to search "upwards" for a file in shell script, e.g. check $PWD, then $PWD/.., then $PWD/../.., etc. until hitting the root. Before I go and roll my own, is there some builtin bash/zsh/find magic that will do this for me?

share|improve this question

Well i have no clue how to do this in zsh since i use bash: but i tried this one liner

START=$PWD; while [ $PWD != "/" ]; \
do ls|grep file && echo $PWD ;cd ..; done ; cd $START

If you want to stop when you find something you could use:

START=$PWD; while [ $PWD != "/" ]; do \
ls|grep file && echo $PWD && cd $START && break;cd ..; done

As far as i know is there no such tool that could do this.

share|improve this answer

Using shell builtins:

(while [[ ! -f $file ]] && [[ $PWD != / ]]; do \cd ..; done;
if [[ -f $file ]];then echo $PWD/$file;fi)
share|improve this answer

Your Answer


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.