Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exec() error when using find -exec

Tags:

c

find

exec

I've to find with a script an executable named Test that take as argument a path, and execute it. I'm doing this with this line:

find -name Test -exec {} path \;

In Test I got an execl:

    execl("./Test1","Test1",(char*)0);
    perror("Exec failed");
    exit(EXIT_FAILURE);

where Test1 is in the same directory of Test . Executing Test "manually" everything goes fine, but using the line written above I have a Exec failed: No such file or directory error.

What's wrong ?

like image 863
cifz Avatar asked Aug 30 '26 09:08

cifz


1 Answers

find executes Test from the directory you are executing find. If you can change the code for Test, then put the absolute path of Test1:

execl("/home/myuser/some/path/Test1","Test1",(char*)0);
perror("Exec failed");
exit(EXIT_FAILURE);

Or you can use -execdir instead of -exec:

find -name Test -execdir {} path \;

From find manpage:

-execdir: Like -exec, but the specified command is run from the subdirectory containing the matched file, which is not normally the directory in which you started find

like image 67
perreal Avatar answered Sep 02 '26 02:09

perreal