I would like to find two types of files (executables and text files) and create soft links for them. My solution so far is:

find src -maxdepth 1 -not -type d -executable -exec ln -fs {} . \;; \
find src -name "*.txt" -exec ln -fs {} . \;

The directory structure is:


I would like to pick up the executables only from the src and the text files *.txt from the lapack and plasma subdirectories.

I would like find a more elegant solution that would combine two find calls into one. I have attempted to use the -o flag, but I encounter problems with the -maxdepth option.


The combined command I'm trying is:

find src -maxdepth 1 -not -type d -executable -o -path src -name "*.txt"

But it does not pick up the text files in the subdirectories. I believe this is due to the -maxdepth 1 flag before the -o.

  • 1
    You can use file command and then do a grep text for text files and grep ELF for executable. – C0deDaedalus May 9 '18 at 12:41
  • 1
    put the command line you've try with -o – Kiwy May 9 '18 at 12:43
  • 1
    -maxdepth is not a test, it is an option. In a find expression, options evaluate to true and affect all tests, including those appearing earlier in the expression. – John Bollinger May 9 '18 at 13:31
  • 1
    Have you considered just leaving it as two commands? That's going to be a lot more readable (IMO) than any variation that does the whole job in a single find command, and that's a strong reason to prefer such an approach. – John Bollinger May 9 '18 at 13:36

You can do it with GNU find or other find that supports -path, but I don't think it's more elegant.

find src \( -type f -executable ! -path 'src/*/*' -o -name '*.txt' \) \
     -exec ln -fs {} . +

You can use zsh instead. Its glob qualifiers are more concise than find syntax.

ln -sf src/*/*(N*.) src/**/*.txt .

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.