设为首页收藏本站

安而遇随-随遇而安

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 633|回复: 0

linux shell 中判断文件、目录是否存在的方法

[复制链接]

 成长值: 31110

发表于 2020-5-27 21:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x

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 如果文件是软链接,则为真

常用例子

如果存在某文件,则删除

  1. if [ -f trials ]; then rm ${result_path}trials; fi
复制代码

如果没有文件夹,则创建

  1. if [ ! -d $result_name ];then
  2.    mkdir -p $result_name
  3. fi
复制代码

shell命令判断文件或文件夹是否存在,先直接看实例:

[size=1em]
  1. #!/bin/sh

  2. #判断文件存在,判断是否为文件夹等
  3. testPath="/Volumes/MacBookProHD/Mr.Wen/08 shell命令"
  4. testFile="/Volumes/MacBookProHD/Mr.Wen/08 shell命令/fileWen"

  5. #判断文件夹是否存在 -d
  6. if [[ ! -d "$testPath" ]]; then
  7. echo "文件夹不存在"
  8. else
  9. echo "文件夹存在"
  10. fi

  11. #判断文件夹是否存在,并且具有可执行权限
  12. if [[ ! -x "$testFile" ]]; then
  13. echo "文件不存在并且没有可执行权限"
  14. else
  15. echo "文件存在并有可执行权限"
  16. fi

  17. #判断文件是否存在
  18. if [[ ! -f "$testFile" ]]; then
  19. echo "文件不存在"
  20. else
  21. echo "文件存在"
  22. fi

复制代码




在shell命令脚本编写当中,会遇到各种对文件的判断,除了以上常用的判断,还有其他的可以使用,如下:


Conditional Logic on Files
-a file exists.
-b file exists and is a block special file.
-c file exists and is a character special file.
-d file exists and is a directory.
-e file exists (just the same as -a).
-f file exists and is a regular file.
-g file exists and has its setgid(2) bit set.
-G file exists and has the same group ID as this process.
-k file exists and has its sticky bit set.
-L file exists and is a symbolic link.
-n string length is not zero.
-o Named option is set on.
-O file exists and is owned by the user ID of this process.
-p file exists and is a first in, first out (FIFO) special file or
named pipe.
-r file exists and is readable by the current process.
-s file exists and has a size greater than zero.
-S file exists and is a socket.
-t file descriptor number fildes is open and associated with a
terminal device.
-u file exists and has its setuid(2) bit set.
-w file exists and is writable by the current process.
-x file exists and is executable by the current process.
-z string length is zero.


shell 判断文件内容是否改变

判断文件内容是否改变:

1)md5值判断

2)diff 判断

  1. #添加日志时间戳
  2. function fn_showlog()
  3. {
  4.   local curtime;
  5.   curtime=`date +"%Y%m%d-%H:%M:%S"`
  6.   echo "$curtime ------ $1";
  7. }
  8. #判断文件内容是否一致
  9. function diff_file(){
  10.   for file in `ls $1`
  11.   do
  12.     if [ ! -f "$2$file" ];then
  13.       fn_showlog "存在新增文件:$2$file"
  14.       return 0
  15.     else
  16.       diff $file $2$file
  17.       if [ $? -ne 0 ];then
  18.         fn_showlog "文件内容发生变化:$file"
  19.         return 0
  20.       fi
  21.     fi
  22.   done
  23.   return 1
  24. }


  25. diff_file "python*Ip" "/home/admin/tools/"
  26. if [ $? -eq 0 ];then
  27.   fn_showlog "内容发生改变......"
  28. else
  29.   fn_showlog "内容未发生改变,退出脚本!"
  30.   exit 0
  31. fi
复制代码





linux shell 中判断文件、目录是否存在的方法

来自:https://www.jb51.net/article/186273.htm


随遇而安
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表