osx find exec rm find: exec: unknown primary or operator
Asked Answered
V

4

45

I've got a bunch of files that end in "-e" that I want to remove.

$ find . -name "*-e" exec rm {} \;
find: exec: unknown primary or operator

Is the regex expanding in some way that messes everything up?

Vigue answered 13/1, 2015 at 5:50 Comment(3)
See this question, though nothing special is needed since {} will expand to ./some/path/-e which can not be treated as an option.Merlynmermaid
I stumbled on this question as I was looking for example uses of find and exec, overlooking the fact that you were doing a rm! PSA: This command will delete the contents of a directory! Use with caution if you're just hacking around. I learned the hard way.Basal
I was forgetting to use quotes around my -name argument value. The * was getting expanded by the shell and not by find.Threatt
S
55

It should be:

find . -name "*-e" -exec rm '{}' \;

Or better:

find . -name "*-e" -exec rm '{}' +

As per man find:

-exec utility [argument ...] {} +
   Same as -exec, except that ``{}'' is replaced with as many pathnames as possible for 
   each invocation of utility. This behaviour is similar to that of xargs(1).
Sherylsheryle answered 13/1, 2015 at 5:52 Comment(4)
Thanks, but I get the same error for both of these. :(Vigue
Me too: $ find . -name *.m4 find: ad.m4: unknown primary or operatorRoseboro
@dba.in.ua: What is output of type find on your shell?Sherylsheryle
For this issue with a find query like find ~ -iname "*.py", I only need to quote the filename sequence for this to work. The {} doesn't need to be quoted when I run itBran
U
15

+1 to @anubhava's answer. However, I did have to be very careful before it worked:

TLDR: the format is -exec <your-command> {} \;

Hint: use something harmless, like echo to get it working first.

find . -name "*.sh" -exec echo {} \;

./081.documentation/000.sticky.list_url_info_markdown/worklocal.sh
./081.documentation/001.fixed.p2.collab_diagrams/common.sh
./081.documentation/001.fixed.p2.collab_diagrams/worklocal.sh

Once it works you can get all fancy with extra find options as in

find . -name "*.sh" -maxdepth 3 -exec echo {} \;

Be sure to use -exec, not --exec or exec

remember that find’s options all take 1 dash. -exec is no different.

find . -name "*.sh" --exec echo {} \;

find: --exec: unknown primary or operator

find . -name "*.sh" exec echo {} \;

find: exec: unknown primary or operator

Notice how what comes after find: reflects what you put in? Here find has no idea what’s going on so you get a generic I don't know message.

Once you are using -exec terminate with \; - that space is critical

find . -name "*.sh" -exec echo {}\;

find: -exec: no terminating ";" or "+"
.................👆 you want to see -exec in your error message. that part is good ✅ find knows what it is looking at so it has -exec related messages.

Finally, replace echo with your actual payload:

find . -name "*.sh" -exec reformat.py --myfancy-option-to-reformat {} \;

'{}' vs {} made no difference whatsoever.

It might, under some conditions, maybe if your paths have spaces in them so if you’re unsure put the quotes. But I couldn't trigger any errors even with spaces and with different commands (cat, ls) than echo.

find . -name "*.sh" -exec echo {} \;

./bar zoom.sh
./foo.sh
./worklocal.sh

find . -name "*.sh" -exec echo '{}' \;

./bar zoom.sh
./foo.sh
./worklocal.sh

I'm not saying the quotes are useless, I am saying they're not causing unknown primary or operator.

ending with + strips newlines between invocations:

Don't forget that space before the +.

find . -name "*.sh" -exec echo {} +

./bar zoom.sh ./foo.sh ./worklocal.sh

Yes, I dream of a more user-friendly find. But macos's Spotlight-related mdfind is, by light-years, even more unfriendly when it comes to options

Utrecht answered 25/4, 2020 at 21:44 Comment(0)
C
3

use find delete args:

find ./ -name "*-e" -delete
Corrinnecorrival answered 14/8, 2020 at 2:41 Comment(0)
R
-3

just use: find . "*-e" -type f exec rm '{}' + it works with Mac OSX find version

Roseboro answered 13/12, 2019 at 14:59 Comment(1)
it doesn't -exec, not exec, is needed.Utrecht

© 2022 - 2024 — McMap. All rights reserved.