也许这段代码可以帮到你:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
char *cmd;
char *folder = "/tmp";
int status, exitcode;
char *format="test $(ls -AU \"%s\" 2>/dev/null | head -1 | wc -l) -ne 0";
clock_t start, stop;
int size;
if(argc == 2)
folder = argv[1];
size = strlen(format)+strlen(folder)+1;
cmd = malloc(size * sizeof(char));
snprintf(cmd, size, format, folder);
printf("executing: %s\n", cmd);
status = system(cmd);
exitcode = WEXITSTATUS(status);
printf ("exit code: %d, exit status: %d\n", exitcode, status);
if (exitcode == 1)
printf("the folder is empty\n");
else
printf("the folder is non empty\n");
free(cmd);
return 0;
}
我使用 ls -AU folder 2>/dev/null | head -1 | wc -l 检查文件夹是否为空,以计算文件夹中的文件数量。如果返回零,则文件夹为空,否则文件夹非空。WEXITSTATUS 宏返回执行命令的退出代码。head 命令不会等待 ls 完成,只需等到条件符合即可。
使用 find 命令生成长文件列表的一些示例表明它确实非常有效。
不带 head 命令的示例:
/usr/bin/time -p -v find / -print | wc -l
output
Command being timed: "find / -print"
User time (seconds): 0.63
System time (seconds): 1.28
Percent of CPU this job got: 98%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:01.94
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 6380
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 3419
Voluntary context switches: 7
Involuntary context switches: 140
Swaps: 0
File system inputs: 0
File system outputs: 0
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Files counted: 1043497
使用 head 修改的命令
/usr/bin/time -p -v find / -print | head -1 | wc -l
Command terminated by signal 13
Command being timed: "find / -print"
User time (seconds): 0.00
System time (seconds): 0.00
Percent of CPU this job got: 100%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.00
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 2864
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 136
Voluntary context switches: 1
Involuntary context switches: 0
Swaps: 0
File system inputs: 0
File system outputs: 0
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Files counted: 1
正如您所看到的,第一个没有“head”的命令执行需要1.28秒,而使用“head”修改后的命令执行需要0秒。
此外,如果我们测量上述核心的执行时间,有和没有头部的区别。
普通的ls:
/usr/bin/time -p ls -A /var/lib/dpkg/info/
real 0.67
user 0.06
sys 0.06
无头程序
/usr/bin/time -p ./empty.exe /var/lib/dpkg/info/
executing: test $(ls -AU "/var/lib/dpkg/info/" 2>/dev/null | wc -l) -ne 0
exit code: 0, exit status: 0
the folder is non empty
real 0.01
user 0.00
sys 0.01
使用头文件的程序
/usr/bin/time -p ./empty.exe /var/lib/dpkg/info/
executing: test $(ls -AU "/var/lib/dpkg/info/" 2>/dev/null | head -1 | wc -l) -ne 0
exit code: 0, exit status: 0
the folder is non empty
real 0.00
user 0.00
sys 0.00
注意:如果文件夹不存在或您没有正确的访问权限,则此程序必须打印“文件夹为空”。
该程序是使用以下命令构建的:gcc empty.c -o empty.exe。
getdents(2)
在许多其他实现中出现,但它不是标准的。 - cnicutar