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,748
  • 3
  • 20
  • 21
  • also this one: [`find` with multiple `-name` and `-exec` executes only the last matches of `-name`](https://unix.stackexchange.com/q/102191/170373), doesn't matter if the action is `-exec` or `-print0` – ilkkachu Jan 02 '22 at 22:40

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,439
  • 1
  • 16
  • 21
  • 2
    Quoting prevents filename expansion, [even in double quotes](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_02_03) (try running `echo "*"`). – Stephen Kitt Jan 03 '22 at 00:15