关于linux,shell脚本中怎样判断文件是否有内容? (linux c 检测目录是否存在)

判断文冲郑纤丛首件大小是不是0kb行不行?使用-s

if ; then

文件内容不为空散仿

else

文件内容为空

fi

可以用“test 条件表达式”进行测试,如:test -f /etc/fstab 测试文件/etc/fstab文件是否存在

-e File 如果文件File存在(Exist),则为True

-s File 如果文件File存在且文件大小(Size)大于零,则为True

-f File 如果文件File存在且是普通文件(File),迹清烂则为True

-d File 如果文件File存在且是目录(Directory),则为True

-b File 如果文件File存在姿漏且是块(Block)特殊文件,则为True

-c File 如果文件File存在且是字符(Character)特殊文件,则为True

-L File 如果文件File存在且是符号链接(Link)文件,则为True

-r File 如果文件File存在且是可读的(Readable),则为True

-w File 如果文件File存在且是可写的(Writable),则为True

-x File 如果文件File存在且是可执行的(Executable),则为True

-O File 如果文件File存在且属于当前用户(Owner),则为True

-G File 如果文件File存在且属于当前用户组(Group),则为True

File1 -nt File2 如果文件正亩File1新于(Newer Then) File2,则为True

File2 -ot File2 如果文件File1旧于(Older Then) File2,则为True

相关问题拓展阅读:

linux c 编程:创建一个线程,监视某个目录,一旦目录里出现新的文件,就将文件转移到指定的目录里去。

/*

头文件

*/

#define SRCPATH “srcpath/”

#define DSTPATH “dstpath/”

int movefile()

{

DIR *dir;

struct dirent *dt;

FILE *fp1,*fp2;

char filename1,filename2;

char buf;

int readsize,writesize;

if((dir = opendir(SRCPATH)) == NULL)

{

printf(“opendir %s error\n”,SRCPATH);

return -1;

}

memset(filename1,0,sizeof(filename1));

strcpy(filename1,SRCPATH);

memset(filename2,0,sizeof(filename2));

strcpy(filename2,DSTPATH);

while(1)

{

while((dt = readdir(dir)) != NULL)

{

if(strcmp(dt->d_name,”.”)==0||strcmp(dt->d_name,”..”)==0)

{

continue;

}

//如果这个目录码孝里 还有目录,可以在这加判断

//这里假设初始为空目录

strcat(filename1,dt->d_name);

strcat(filename2,dt->d_name);

//如果进程碧拆资源较少悔模枣可以直接用linux系统命令

fp1 = fopen(filename1,”rb”);

if(fp1==NULL)

{

printf(“open %s failed /n”,filename1);

return -1;

}

fp2 = fopen(filename2,”wb”);

if(fp2==NULL)

{

printf(“open %s failed /n”,filename2);

fclose(fp1);

return -1;

}

while((readsize = fread(buf,sizeof(buf),1,fp1))>0)

{

//total += readsize;

memset(buf,0,sizeof(buf));

writesize = fwrite(buf,sizeof(buf),1,fp2);

if(writesize!==readsize)

{

printf(“write error”);

return -2;

fclose(fp1);

fclose(fp2);

}

}

fclose(fp1);

fclose(fp2);

rmdir(filename2);

}

}

}

int main(int argc,char **argv)

{

pthread_t id1;

int ret;

ret = pthread_create(&id1, NULL, (void*)movefile, NULL);

return ret;

}

关于linux c 检测目录是否存在的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。


数据运维技术 » 关于linux,shell脚本中怎样判断文件是否有内容? (linux c 检测目录是否存在)