神剑山庄资源网 Design By www.hcban.com
本文主要介绍了linux shell 中判断文件、目录是否存在的方法,分享给大家
-e filename 如果 filename存在,则为真
-d filename 如果 filename为目录,则为真
-f filename 如果 filename为常规文件,则为真
-L filename 如果 filename为符号链接,则为真
-r filename 如果 filename可读,则为真
-w filename 如果 filename可写,则为真
-x filename 如果 filename可执行,则为真
-s filename 如果文件长度不为0,则为真
-h filename 如果文件是软链接,则为真
常用例子
如果存在某文件,则删除
if [ -f trials ]; then rm ${result_path}trials; fi
如果没有文件夹,则创建
if [ ! -d $result_name ];then mkdir -p $result_name fi
shell命令判断文件或文件夹是否存在,先直接看实例:
#!/bin/sh #判断文件存在,判断是否为文件夹等 testPath="/Volumes/MacBookProHD/Mr.Wen/08 shell命令" testFile="/Volumes/MacBookProHD/Mr.Wen/08 shell命令/fileWen" #判断文件夹是否存在 -d if [[ ! -d "$testPath" ]]; then echo "文件夹不存在" else echo "文件夹存在" fi #判断文件夹是否存在,并且具有可执行权限 if [[ ! -x "$testFile" ]]; then echo "文件不存在并且没有可执行权限" else echo "文件存在并有可执行权限" fi #判断文件是否存在 if [[ ! -f "$testFile" ]]; then echo "文件不存在" else echo "文件存在" fi
在shell命令脚本编写当中,会遇到各种对文件的判断,除了以上常用的判断,还有其他的可以使用,如下:
shell 判断文件内容是否改变
判断文件内容是否改变:
1)md5值判断
2)diff 判断
#添加日志时间戳 function fn_showlog() { local curtime; curtime=`date +"%Y%m%d-%H:%M:%S"` echo "$curtime ------ $1"; } #判断文件内容是否一致 function diff_file(){ for file in `ls $1` do if [ ! -f "$2$file" ];then fn_showlog "存在新增文件:$2$file" return 0 else diff $file $2$file if [ $"文件内容发生变化:$file" return 0 fi fi done return 1 } diff_file "python*Ip" "/home/admin/tools/" if [ $"内容发生改变......" else fn_showlog "内容未发生改变,退出脚本!" exit 0 fi
神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com
暂无linux shell 中判断文件、目录是否存在的方法的评论...
更新日志
2024年10月04日
2024年10月04日
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[320K/MP3][203.44MB]
- 群星.1981-名曲65(2014环球复黑王·百代篇)【EMI百代】【WAV+CUE】
- 陈淑桦.1990-娃娃的故事【柯达】【WAV+CUE】
- 戴梅君.2011-问签诗【美华】【WAV+CUE】
- 戴梅君.2011-问签诗【美华】【WAV+CUE】
- 李国祥.1995-九五变奏【嘉音】【WAV+CUE】
- 许景淳.1992-你来自何方【全美唱片】【WAV+CUE】
- 石欣卉.2007-剧欣卉集·完整电视剧主题精丫华纳】【WAV+CUE】
- 群星.2005-LOVE情歌集VOL.5.2CD【正东】【WAV+CUE】
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】