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

本文详细介绍了在Linux Shell脚本中如何判断文件和目录是否存在,包括使用诸如`-e`, `-d`, `-f`, `-L`等条件测试符。文章还提供了多个实用示例,如根据文件是否存在进行删除操作,以及创建目录等。此外,还展示了如何判断文件的可读、可写、可执行性以及文件大小。最后,文章提到了文件内容变化的检测方法,如通过md5值和diff命令。
摘要由CSDN通过智能技术生成

这篇文章主要介绍了linux shell 中判断文件、目录是否存在的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

本文主要介绍了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命令判断文件或文件夹是否存在,先直接看实例:

    
    
  1. #!/bin/sh
  2.  
  3. #判断文件存在,判断是否为文件夹等
  4. testPath="/Volumes/MacBookProHD/Mr.Wen/08 shell命令"
  5. testFile="/Volumes/MacBookProHD/Mr.Wen/08 shell命令/fileWen"
  6.  
  7. #判断文件夹是否存在 -d
  8. if [[ ! -d "$testPath" ]]; then
  9. echo "文件夹不存在"
  10. else
  11. echo "文件夹存在"
  12. fi
  13.  
  14. #判断文件夹是否存在,并且具有可执行权限
  15. if [[ ! -x "$testFile" ]]; then
  16. echo "文件不存在并且没有可执行权限"
  17. else
  18. echo "文件存在并有可执行权限"
  19. fi
  20.  
  21. #判断文件是否存在
  22. if [[ ! -f "$testFile" ]]; then
  23. echo "文件不存在"
  24. else
  25. echo "文件存在"
  26. fi
  27.  

在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.  
  26.  
  27. diff_file "python*Ip" "/home/admin/tools/"
  28. if [ $? -eq 0 ];then
  29. fn_showlog "内容发生改变......"
  30. else
  31. fn_showlog "内容未发生改变,退出脚本!"
  32. exit 0
  33. fi

到此这篇关于linux shell 中判断文件、目录是否存在的方法的文章就介绍到这了,更多相关linux shell判断文件存在内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

本文标题: linux shell 中判断文件、目录是否存在的方法
本文地址: http://www.cppcns.com/os/linux/312982.html
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值