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

It must exists, my problem is how to express my need. When I search for a given filename, Iwant the search stop searching the current branch when it finds a first occurence, BUT continue to search in other branches.

Here an example. This is my current directory:


The result I want with find Makefile is:


I don't mind which script language to use, but I prefer an existing function in a script language instead of implementing my own.

share|improve this question

As I really can't find anything, I made my own (in python)


import os

def findInSubdirectory(filename, path):
    for root, dirs, names in os.walk(path, topdown=True):
        if filename in names:
            print os.path.join(root, filename)
            del dirs[:]

findInSubdirectory('Makefile', os.getcwd())
share|improve this answer

You can use the find command like this:

$ find . -type d -exec test -e {}/Makefile \; -print -prune

In detail:

$ find .                            # find starting in the current directory \
       -type d                      # only look at directories \
       -exec test -e {}/Makefile \; # test if Makefile exists in the directory \
       -print                       # print the directory name and path \
       -prune                       # stop searching once we find a Makefile

(not sure if this would actually run with the comments/line continuations...)

The best bit is that since it's find, you can do cleverer things than just -print the name.

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.