0

TL;dr: How do I properly combine the -print0 option in find with the -o option to match multiple patterns? (The use case is to pass into xargs -0)

Example:

find . -print0 -name "File*.dat" -o -name "Data*.txt"
find . -print0 -name "File*.dat" -o -print0 -name "Data*.txt"

Both of these return every file in the directory.

find . -name "File*.dat" -o -name "Data*.txt" -print0

This returns only files matching the second pattern (Data*.txt).

How can I do this properly, and why does this happen?

fdmillion
  • 2,828

1 Answers1

-1

You have quoting problems, and a grouping problem. Using """ invites the shell to do a wildcard match. If you have a matching file or in the current directory, the wildcard will be replaced. Use "'" instead.
Grouping is done with parentheses "()" which need to be shell escaped.
Your command should be.

find . \( -name 'File*.dat' -o -name 'Data*.txt' \) -print0

Read man find.

waltinator
  • 4,865