10

I am trying to find some files inside a directory. By default, the find command is searching the directory which I have specified and it's sub directories.

I tried to use depth/maxdepth and prune, but nothing helped to overcome this. Can somebody point out the right way to use depth/prune in Solaris?

My code looks like this:

find file_path -depth 1 -name '*.log'

I am getting the following error:

find: bad option 1
find: [-H | -L] path-list predicate-list

migrated from stackoverflow.com Aug 8 '11 at 11:17

This question came from our site for professional and enthusiast programmers.

15

You're confusing two unrelated options that have vaguely similar names:

  • -depth doesn't take any argument. If present, it tells find to process the contents of a directory before processing the directory itself.
  • -maxdepth N (where N is an integer) limits the recursion to N levels of subdirectories.

The -maxdepth option is an extension found in some versions of find, but not Solaris's. There's a trick to using only standard options to find to process a directory without recursing: use -prune on subdirectories, but exclude the toplevel directory. Since the only way to match the toplevel directory is by name, arrange for the name to be . (which will never be the name of a subdirectory).

find /directory/to/traverse/. -name . -o \
                              -type d -prune -o \
                              -name '*.log' -print
  • I was facing this very problem, and this solution helped. I spent a day trying to find a solution to getting this to work. Grrr, Solaris. – Kevin Teljeur Sep 21 '16 at 10:06
  • Do you see any issue using sed to remove the /./ find ${directory}/sh/. -name . -o -type d -prune -o -print | sed 's/\/\.\//\//' – Ethan Post Aug 8 '18 at 0:39
  • @EthanPost You can do that, but find implementations without -mindepth and -maxdepth are a vanishing breed. If your find has -maxdepth, use find /directory/to/traverse -mindepth 1 -maxdepth 1 -name '*.log' – Gilles Aug 8 '18 at 5:52
  • @Gilles Just takes one to break you when you are building a framework that may need to run in just such a circumstance! That market may be my bread and butter someday! Thanks for the help. – Ethan Post Aug 8 '18 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.