Find command, -name by directory and subdirectory?

Hi All,

I'm trying to use the find command to return matches for a directory and file.

For example, given the following directories:

/one/two/three/file1.txt
/one/three/two/file1.txt
/one/four/two/three/file1.txt

I'm expecting the following to be returned:

/one/two/three/file1.txt
/one/four/two/three/file1.txt

I've tried the following with no luck:
find / -name three/file1.txt
find / \( -name three-a -name file1.txt \)

Any way to get this accomplished using the find command? I don't want find going down each directory, so grep will not work.

Thanks!

#!/bin/ksh
find /one/ -type f -name file1\.txt -print | while read FILENAME
do
       # Is the directory name above the filename "three" ?
       THREE1="`dirname ${FILENAME}`"
       THREE="`basename ${THREE1}`"
        if [ "${THREE}" = "three" ]
        then
               echo "${FILENAME}"
        fi
done

Thanks Methyl, but I don't want find recursing through all directories and I ideally want this performed within the find command.

Another approach where "find" only looks in the target directories:

find /one/two/three/ -type f -name 'file1.txt' -print
find /one/four/two/three/ -type f -name 'file1.txt' -print

Methyl, I am looking for this to be performed in a single find command, as I will not have knowledge of directories which match the search criteria before hand.

try this

find / -wholename "*three/file1.txt" -print

(withdrawn)

Or something like:

find /one/[tf][wo]* -name file1.txt

Find has to "go down each directory" to find things.

We need to pre-process the directory list and look only at directories which are called "three" AND are at the bottom of a directory tree. The issue arises with "find" processing intermediate directories:

/one
/one/three                 We don't want to search this one.
/one/three/two           Or this one

I have a convoluted method to find the bottom of the tree which counts the number of directories in each directory. If there are only two directories you are at the bottom of the tree. I won't post that because there has to be a better way!

Simples one I can think of, based on the example:

find one -type d -name three -exec ls {}/file1.txt \; 2>/dev/null

This is an improvement but it will find a file "file1.txt" in "/one/three" when there is a subdirectory "/one/three/two".

Does the directory have to start with one, and end with three?

find /one/*/three -maxdepth 1 -name file1.txt

The o/p said what he wants, but not what he doesn't want (apart from not using grep)!

Hi scottn. My interpretation of the original post is that the tree has to end in ".../three/file1.txt" with no subdirectories under ".../three/".
I don't think we know what version of "find".
Also the example given is not the requirement:

Unfortunately this is not portable. Maxdepth is not universal.

Judging by the examples posted if the directory is "three" and contains the file that's it, there was no maximum depth criteria set.

I appreciate everyone's help. I was surprised by the number of responses :slight_smile:

The following will work fine if somone knows of a way to specify only the last 2 child most subdirectories in the find directory path (there can be a variable number of parent directories unfortunately, so I can't just '/*/*/child/child it):

find /one_or_more_parent_dirs/2nd_last_child_dir/last_child_dir -name file1.txt

Clear as mud.
It would really help us "makodarear" if you could present a syntactically correct example mentioning the directory "three" and the filename "file1" and making it emphatically clear (preferably with examples) what constitutes a match.
The reason that there are many reponses is because the requirement is ambiguous

Ps. Knowing what Operating System and Shell you are using would concentrate the mind and give better a chance of a specialist responding.

Sounds like you might want to:

- Parse the entire directory tree with find - report only on directories.
- use awk to split the result of find using "\" as the field seperator
    if the last field of the awk result matches your directory variable
    then proceed to check THAT directory for the file
- return all instances where this is a match.

Yes?