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.

I got one probably silly question:

I'm working on remote server under Solaris using ssh and my task is to find specific files in directories matching shell pattern but not go recursively deeper.

Suppose I have following directory hierarchy:

dir-a/
    file-a
    file-b
    dir-c/
        file-a
dir-b/
    file-a
    file-b

and I want to find all files named file-a in directories dir-a & dir-b but not in directory dir-c. Ok, so far so good - it's really simple with single find command:

 find dir-{a,b} -mindepth 1 -maxdepth 1 -name 'file-a'

but the problem is that on this machine installed really old version of find so it doesn't has -mindepth and -maxdepth options. I tried to use -prune:

find dir-{a,b} \( -type d -prune \) -o -name file-a -print 

but it prints nothing at all cause very first directories in search tree are dir-a and dir-b themselves so find just skip their contents as it actually should. If I could use -mindepth I would merely skip this "root" directories with -mindepth 1 and get another working solution, but as I said I can't. depth didn't help also because dir-a and dir-b are last in find search tree if it used and dir-a/dir-c/file-a is now again in output. Oh and this old find also hasn't -regex and -wholepath option so I can't use complex name patterns to exclude deeper files form results. Do you have any assumptions?

share|improve this question

3 Answers 3

You can do this without using find

ls dir-[ab]/file-a

I notice that SunFreeware has GNU findutils packages for Solaris.

share|improve this answer
    
yes I could but I suppose to use more complex test than just name equality (for example, date of last access and file type) and do some work with list of finding results then. Example is only for clarity) –  Mikhail Golubev Sep 15 '11 at 21:37

This seems to do what you want:

find dir-a dir-b -type d ! -name dir-a ! -name dir-b -prune -o -name file-a -print

Here is a break down:

-type d ! -name dir-a ! -name dir-b -prune

Any directory that is not the name dir-a or dir-b are pruned.

-name file-a -print

Then print any other object that is named 'file-a', but then this should be obvious.

I'm guessing that the version of Solaris you are using does not have the -not operator, so use the older ! operator instead.

The drawback is that you will need to add ! -name clauses for each directory entered on the command-line. But you could generate a command to set this up more generically.

share|improve this answer

You may use grep(1) to overcome the lack of -mindepth and -maxdepth on the find(1) implementation used by Solaris.

Not very pretty, but an easy and fast solution, if you're limited in the choice of software installed (i.e. sitting in front of a customer's machine):

$ find . -name test
./test
./test/test
./test/test/test
./test/test/test2/test

$ find . -name test | grep -v '/.*/.*/' | grep '/.*/' 
./test/test

You may save the cost of one process by using awk(1):

$ find . -name test | awk -F/ 'NF == 3'
./test/test
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.