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?
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?
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).
type find
on your shell? –
Sherylsheryle 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 it –
Bran +1 to @anubhava's answer. However, I did have to be very careful before it worked:
-exec <your-command> {} \;
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 {} \;
-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.
-exec
terminate with \;
- that space is criticalfind . -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.
echo
with your actual payload:find . -name "*.sh" -exec reformat.py --myfancy-option-to-reformat {} \;
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.
+
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
use find delete args:
find ./ -name "*-e" -delete
just use: find . "*-e" -type f exec rm '{}' +
it works with Mac OSX find version
-exec
, not exec
, is needed. –
Utrecht © 2022 - 2024 — McMap. All rights reserved.
{}
will expand to./some/path/-e
which can not be treated as an option. – Merlynmermaidfind
andexec
, overlooking the fact that you were doing arm
! 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-name
argument value. The*
was getting expanded by the shell and not byfind
. – Threatt