Shell文件属性的判断与比较

Shell支持对文件属性的判断,常用的文件属性操作符很多,如下表所示。更多文件属性操作符可以参考命令帮助手册man test

Shell文件属性的判断与比较
[root@centos7~]# touch file1.txt
[root@centos7~]# touch file2.txt
[root@centos7~]# mkdir test
[root@centos7~]# [ -e file1.txt ] && echo对|| echo 错   #判断文件是否存在对
[root@centos7~]# [ -e test ] && echo对|| echo 错       #判断目录是否存在对
[root@centos7~]# [ ! -e file1.txt ] && echo对|| echo 错 #判断文件是否不存在错
[root@centos7~]# [ -f file1.txt ] && echo对|| echo 错   #判断存在,且为文件对
[root@centos7~]# [ ! -f file1.txt ] && echo对|| echo 错 #判断该文件不存在错
[root@centos7~]# [ -f test/ ] && echo对|| echo 错      #因为不是文件,结果错错

[root@centos7~]# [ -r file1.txt ] && echo Y || echo NY
[root@centos7~]# chmod -r file1.txt                        #删除r权限
[root@centos7~]# [ -r file1.txt ] && echo Y || echo N      #测试结果依然为真Y
[root@centos7~]# [ ! -r file1.txt ] && echo Y || echo N   #测试不可读N
[root@centos7~]# chmod -w file1.txt                        #删除w权限
[root@centos7~]# ls -l file1.txt----------. 1 root root 0 8月 16 20:31 file1.txt
[root@centos7~]# [ -w file1.txt ] && echo Y || echo N      #测试结果依然为真Y
[root@centos7~]# [ -x file1.txt ] && echo Y || echo N      #测试结果为假N
[root@centos7~]# chmod +x file1.txt                        #添加x权限
[root@centos7~]# ls -l file1.txt---x--x--x. 1 root root 0 8月 16 20:31 ver1.txt
[root@centos7~]# [ -x file1.txt ] && echo Y || echo NY

Original: https://www.cnblogs.com/xiong97/p/16625866.html
Author: 静言善思
Title: Shell文件属性的判断与比较

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/612267/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球