294

I was helped out today with a command, but it doesn't seem to be working. This is the command:

find /home/me/download/ -type f -name "*.rm" -exec ffmpeg -i {} -sameq {}.mp3 && rm {}\;

The shell returns

find: missing argument to `-exec'

What I am basically trying to do is go through a directory recursively (if it has other directories) and run the ffmpeg command on the .rm file types and convert them to .mp3 file types. Once this is done, remove the .rm file that has just been converted.

alfC
  • 14,261
  • 4
  • 67
  • 118
Abs
  • 56,052
  • 101
  • 275
  • 409

13 Answers13

448

A -exec command must be terminated with a ; (so you usually need to type \; or ';' to avoid interpretion by the shell) or a +. The difference is that with ;, the command is called once per file, with +, it is called just as few times as possible (usually once, but there is a maximum length for a command line, so it might be split up) with all filenames. See this example:

$ cat /tmp/echoargs
#!/bin/sh
echo $1 - $2 - $3
$ find /tmp/foo -exec /tmp/echoargs {} \;
/tmp/foo - -
/tmp/foo/one - -
/tmp/foo/two - -
$ find /tmp/foo -exec /tmp/echoargs {} +
/tmp/foo - /tmp/foo/one - /tmp/foo/two

Your command has two errors:

First, you use {};, but the ; must be a parameter of its own.

Second, the command ends at the &&. You specified “run find, and if that was successful, remove the file named {};.“. If you want to use shell stuff in the -exec command, you need to explicitly run it in a shell, such as -exec sh -c 'ffmpeg ... && rm'.

However you should not add the {} inside the bash command, it will produce problems when there are special characters. Instead, you can pass additional parameters to the shell after -c command_string (see man sh):

$ ls
$(echo damn.)
$ find * -exec sh -c 'echo "{}"' \;
damn.
$ find * -exec sh -c 'echo "$1"' - {} \;
$(echo damn.)

You see the $ thing is evaluated by the shell in the first example. Imagine there was a file called $(rm -rf /) :-)

(Side note: The - is not needed, but the first variable after the command is assigned to the variable $0, which is a special variable normally containing the name of the program being run and setting that to a parameter is a little unclean, though it won't cause any harm here probably, so we set that to just - and start with $1.)

So your command could be something like

find -exec bash -c 'ffmpeg -i "$1" -sameq "$1".mp3 && rm "$1".mp3' - {} \;

But there is a better way. find supports and and or, so you may do stuff like find -name foo -or -name bar. But that also works with -exec, which evaluates to true if the command exits successfully, and to false if not. See this example:

$ ls
false  true
$ find * -exec {} \; -and -print
true

It only runs the print if the command was successfully, which it did for true but not for false.

So you can use two exec statements chained with an -and, and it will only execute the latter if the former was run successfully.

Marian
  • 5,817
  • 2
  • 18
  • 21
  • 4
    The key seems to be Marian's line of "the ; is an arg on it's own" that is what did it for me, lightbulb wise and for my code sample. thanks. – pjammer Mar 20 '14 at 01:29
  • 4
    That's about the best description i've read on -exec. It's extremely powerful but I always find it difficult to get the right syntax for it. This made a few things much clearer. Particularly wrapping the command in the separate shell. Nice. Thanks. – Eurospoofer Jan 27 '16 at 10:10
  • 4
    Note that `-and` and `-or` are not portable. [POSIX specifies `-a` and `-o`](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/find.html), and in fact `-a` is always assumed (hence not needed). – gniourf_gniourf Oct 02 '16 at 12:38
  • Also there must be a space before the terminator. Ex `"\;"` doesn't work but `" \;"` does – frmdstryr Mar 06 '19 at 22:42
  • For the benefit of future readers who encounter this error with ssh, enclosing the terminating escaped-semicolon after the -exec resolved the issue(Ubuntu 18.04.2 LTS): $ ssh remuser@remhost find Documents -exec echo {} '\;' – Snidhi Sofpro Feb 11 '21 at 07:23
80

Try putting a space before each \;

Works:

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

Doesn't Work:

find . -name "*.log" -exec echo {}\;
Dustin Cowles
  • 976
  • 7
  • 8
  • 3
    This trips me up quite often. One of these days, I'll add a space by default. – harperville Apr 22 '15 at 20:48
  • For me it works just the opposite in Cygwin under Windows 7: no space before\ ; works, with space - doesn't. But if i remove \, with space before ; it works, and without space before ; it doesn't, just the way it described here. – WebComer Feb 10 '17 at 23:58
68

I figured it out now. When you need to run two commands in exec in a find you need to actually have two separate execs. This finally worked for me.

find . -type f -name "*.rm" -exec ffmpeg -i {} -sameq {}.mp3 \; -exec rm {} \;
Abs
  • 56,052
  • 101
  • 275
  • 409
  • Not sure if it will get the variable of the file to delete? Anyone know if this is the case? – Abs Jun 02 '10 at 21:46
  • 9
    To test such things just add an `echo` before the commands and see what it does. – Marian Jun 02 '10 at 22:22
  • 3
    @Marian, `echo` is quite unreliable -- you can't tell the difference between `echo "one argument"` and `echo one argument` (the latter's output being false, as it's actually passing `echo` two completely separate arguments). Much better to do something like `-exec bash -c 'printf "%q " "$@"' _ ffmpeg -i {} -sameq {}.mp3 \; -printf '\n'`, which will print output in a way that makes nonprintable characters visible so you can detect DOS newlines and other oddities. – Charles Duffy Apr 20 '18 at 17:34
14

You have to put a space between {} and \;

So the command will be like:

find /home/me/download/ -type f -name "*.rm" -exec ffmpeg -i {} -sameq {}.mp3 && rm {} \;
Pablo Bianchi
  • 1,824
  • 1
  • 26
  • 30
AzafoCossa
  • 874
  • 8
  • 18
8

Just for your information:
I have just tried using "find -exec" command on a Cygwin system (UNIX emulated on Windows), and there it seems that the backslash before the semicolon must be removed:
find ./ -name "blabla" -exec wc -l {} ;

Dominique
  • 16,450
  • 15
  • 56
  • 112
  • 1
    I really confused. `find /etc/nginx -name '*.conf' -exec echo {} ;` and `find /etc/nginx -name '*.conf' -exec echo {}\;` gave the same result. :( – Kirby Mar 14 '16 at 22:19
  • I'm running the Bash shell that comes with Git for Windows, and Dustin Cowles' answer works for me. In other words: no quotes, backslash escaped by semicolon, space after `{}`. – mamacdon Jan 09 '18 at 20:10
  • There's no difference in meaning, but in some cases you need to put the backslash, in some case you can't put it, and in again other cases you might choose. – Dominique Oct 08 '18 at 14:08
5

For anyone else having issues when using GNU find binary in a Windows command prompt. The semicolon needs to be escaped with ^

find.exe . -name "*.rm" -exec ffmpeg -i {} -sameq {}.mp3 ^;
jansohn
  • 2,246
  • 2
  • 28
  • 40
3

You need to do some escaping I think.

find /home/me/download/ -type f -name "*.rm" -exec ffmpeg -i {} \-sameq {}.mp3 \&\& rm {}\;
Matthew Smith
  • 1,287
  • 1
  • 9
  • 19
3

Just in case anyone sees a similar "missing -exec args" in Amazon Opsworks Chef bash scripts, I needed to add another backslash to escape the \;

bash 'remove_wars' do
  user 'ubuntu'
  cwd '/'
  code <<-EOH
    find /home/ubuntu/wars -type f -name "*.war" -exec rm {} \\;
  EOH
  ignore_failure true
end
John Cooper
  • 151
  • 1
  • 1
3

Also, if anyone else has the "find: missing argument to -exec" this might help:

In some shells you don't need to do the escaping, i.e. you don't need the "\" in front of the ";".

find <file path> -name "myFile.*" -exec rm - f {} ;
sellmaurer
  • 293
  • 2
  • 6
  • 2
    The `;` not being quoted or escaped here means it can be consumed by the shell, not read by `find`. Also, `-f` and `- f` are two different things. – Charles Duffy Apr 20 '18 at 17:32
1

Both {} and && will cause problems due to being expanded by the command line. I would suggest trying:

find /home/me/download/ -type f -name "*.rm" -exec ffmpeg -i \{} -sameq \{}.mp3 \; -exec rm \{} \;
VeeArr
  • 6,039
  • 3
  • 24
  • 45
  • If the first command in exec succeeds and the second command in exec is executed, will it still have access to the `{}` variable to delete the right file? – Abs Jun 02 '10 at 21:54
  • What do you thinik `{}` expands to? Unless it contains two dots or commas that will stay as it is. – Marian Jun 02 '10 at 22:21
0

In my case I needed to execute "methods" from by bash script, which does not work when using -exec bash -c, so I add another solution I found here, as well:

UploadFile() {
  curl ... -F "file=$1"
}

find . | while read file;
  do
    UploadFile "$file"
  done

This thread pops up first when searching for solutions to execute commands for each file from find, so I hope it's okay that this solution does not use the -exec argument

hb0
  • 3,350
  • 3
  • 30
  • 48
0

I got the same error when I left a blank space after the ending ; of an -exec command.
So, remove blank space after ;

alboforlizo
  • 170
  • 1
  • 6
-3

If you are still getting "find: missing argument to -exec" try wrapping the execute argument in quotes.

find <file path> -type f -exec "chmod 664 {} \;"
woo
  • 11
  • `find: In ‘-exec ... {} +’ the ‘{}’ must appear by itself, but you specified ‘... {} ...’` – Pysis Aug 16 '21 at 23:26