execl command

how to use find command in execl function,

I used:

execl("/usr/bin/find","find","~","-name","filename.c",0);

but it shows

find: ~ no file and directory

i need to get the path of the file from the home .:wall:

I think find /<space>-name <filename> will help u :slight_smile:

"~" is a shortcut that shells (like bash) implement. At the "C" api level, you can only use actual relative or absolute paths (no environment variables or anything).
If you wish to implement functionality similar to "~" you must do something like:

#include <pwd.h>
.
.
struct passwd *pwd=getpwuid(getuid());
if(pwd && pwd->pw_dir) {
    execl("/usr/bin/find","find",pwd->pw_dir,"-name","filename.c",0);
} else {
    fprintf(stderr,"Cannot find home directory\n");
}
1 Like