也许这段代码可以帮助你:
#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 |头-1 | wc -l,计算文件夹中的文件,如果返回零则文件夹为空,否则文件夹非空。 WEXITSTATUS 宏,返回执行命令的退出代码。 head 命令不会等到 ls 完成,而是等到条件满足。
使用 find 命令生成长文件列表的一些例子表明它确实有效
无头命令
/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
使用 head 编程
/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 构建的