Search code examples
clinuxfindexecl

execl + find + -exec: missing argument to `-exec'


I am trying to run execlp with find ... -exec ..., and the find program consistently tells me:

find: missing argument to `-exec'

What could be wrong? When I run find with these arguments on my shell, it succeeds.

My function calls follow (after looking at related SO threads, I have tried several arrangements of the argmuments):

execlp("find","find","/home/me","-exec","/usr/bin/stat", "{}", "\\;",NULL);
execlp("find","find","/home/me","-exec","/usr/bin/stat", "'{}'", "\\;",NULL);
execlp("find","find","/home/me","-exec","/usr/bin/stat", "{}", "';'",NULL);
execlp("find","find","/home/me","-exec","/usr/bin/stat {} \\;",NULL);

Solution

  • When you execute the command from C, you don't need the \ before ;

    Using this syntax should work

    execlp("find","find","/home/me","-exec","/usr/bin/stat", "{}", ";",NULL);
    

    When on the shell, ; marks the end of a command, and has to be escaped. execlp doesn't go through the shell to execute a command, it does it immediately.


    Edit -- you actually only do one find so this part is not necessary

    Moreover, the exec family replaces the current process with the requested command. So only the first execlp will be executed.

    A solution is to fork() for each find (one by one, waiting for the child process to end, otherwise the output will be a mix of all results).