Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

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:

dir1/Makefile
dir2/
dir3/Makefile
dir3/dira/Makefile
dir4/dirb/Makefile

The result I want with find Makefile is:

dir1/Makefile
dir3/Makefile
dir4/dirb/Makefile

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)

#!/usr/bin/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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .