Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

2 Answers 2

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

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.