find -name "*.xyz" -o -name "*.abc" -exec to Execute on all found files, not just the last suffix specified

LinuxBashShellScripting

Linux Problem Overview


I'm trying to run

find ./ -name "*.xyz" -o -name "*.abc" -exec cp {} /path/i/want/to/copy/to

In reality it's a larger list of name extensions but I don't know that matters for this example. Basically I'd like to copy all those found to another /path/i/want/to/copy/to. However it seems to only be executing the last -name test in the list.

If I remove the -exec portion all the variations of files I expect to be found are printed out.

How do I get it to pass the full complement of files found to -exec?

Linux Solutions


Solution 1 - Linux

find works by evaluating the expressions you give it until it can determine the truth value (true or false) of the entire expression. In your case, you're essentially doing the following, since by default it ANDs the expressions together.

-name "*.xyz" OR ( -name "*.abc" AND -exec ... )

Quoth the man page:

> GNU find searches the directory tree rooted at each given file name by evaluating the given expression from left to right, according to the rules of precedence (see section OPERATORS), until the outcome is known (the left hand side is false for and operations, true for or), at which point find moves on to the next file name.

That means that if the name matches *.xyz, it won't even try to check the latter -name test or -exec, since it's already true.

What you want to do is enforce precedence, which you can do with parentheses. Annoyingly, you also need to use backslashes to escape them on the shell:

find ./ \( -name "*.xyz" -o -name "*.abc" \) -exec cp {} /path/i/want/to/copy/to \;

Solution 2 - Linux

More usable than Jaypal's solution would maybe be:

   find ./ -regex ".*\.\(jpg\|png\)" -exec cp {} /path/to

Solution 3 - Linux

find . \( -name "*.xyz" -o -name "*.abc" \) -exec cp {} /path/i/want/to/copy/to \;

Solution 4 - Linux

It may work:

find ./ -name "*.{xyz,abc}" -exec cp {} /path/i/want/to/copy/to

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionatxdbaView Question on Stackoverflow
Solution 1 - LinuxDan FegoView Answer on Stackoverflow
Solution 2 - Linuxuser unknownView Answer on Stackoverflow
Solution 3 - LinuxRob WoutersView Answer on Stackoverflow
Solution 4 - LinuxchemilaView Answer on Stackoverflow