#!/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
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.
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!
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:
I appreciate everyone's help. I was surprised by the number of responses
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):
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.
- 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.