c - 使用 execv 查找异常结果

标签 c linux find exec

if (pid == 0) {
    char * para[] = {"find", "./", "*.c", NULL};
    execv("/usr/bin/find", para);
    printf("execu failed: %s\n", strerror(errno)); //ls -l -R
    exit(-1);
}

当我尝试使用 execv() 中的查找时,我收到消息 ./: ‘*.c’: No such file or directory。当我在没有 *.c 参数的情况下运行 execv(find...) 时,我得到了编译我的程序的文件夹的内容,包括一些 .c文件 ./test.c, ./rock_paper_scissors.c, ./main.c 等等。当我直接在终端中运行命令 find ./*.c 时,我得到了预期的结果,即该目录中的所有 .c 文件。

是否有一些我没有看到的不同方式要求我通过 exec 使用查找?

最佳答案

改变

char * para[] = {"find", "./", "*.c", NULL};

成为

char * para[] = {"find", "./", "-name", "*.c", NULL};

来自 man find :

-name pattern

Base of file name (the path with the leading directories removed) matches shell pattern pattern. Because the leading directories are removed, the file names considered for a match with -name will never include a slash, so -name a/b will never match anything (you probably need to use -path instead).

关于c - 使用 execv 查找异常结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39824872/

相关文章:

c - 使用 GoogleMock 指定输出字符串参数

swift - 无法将 Swift 添加到 PATH Linux

php - 从源代码编译 PHP 5.1.6 - php -v 和 phpinfo() 版本差异

matlab - 查找 3D 矩阵中的索引

c# - 查找谓词 DayOfWeek Issue c#

c - Linux 中没有 O_BINARY 和 O_TEXT 标志?

c - 在 C 中写入文件时不需要的字符

c - 使用 fscanf() 读取文本文件时出错

linux - 识别从串行终端读取时\r 伪影的原因?

arrays - 在一组树中找到正方形