Linux shell文件属性的判断与比较
作者:优品建站     
发布日期:2022-04-23     
访问量:61
Shell支持大量对文件属性的判断,常用的文件属性操作符很多,如表2-2所示。更多文件属性操作符可以参考命令帮助手册(man test)。
表2-2 文件属性操作符
首先,需要创建几个用于演示的文件和目录,注意创建文件时最好间隔一定的时间,让两个文件的最后修改时间有点间隔。
[root@centos7~]# touch ver1.txt
[root@centos7~]# touch ver2.txt
[root@centos7~]# mkdir test
[root@centos7~]# [ -e ver1.txt ] && echo对|| echo 错 #判断文件是否存在
对
[root@centos7~]# [ -e test ] && echo对|| echo 错 #判断目录是否存在
对
[root@centos7~]# [ ! -e ver1.txt ] && echo对|| echo 错 #判断文件是否不存在
错
[root@centos7~]# [ -f ver1.txt ] && echo对|| echo 错 #判断存在,且为文件
对
[root@centos7~]# [ ! -f ver1.txt ] && echo对|| echo 错 #判断该文件不存在
错
[root@centos7~]# [ -f test/ ] && echo对|| echo 错 #因为不是文件,结果错
错
[root@centos7~]# [ -d test/ ] && echo对|| echo 错 #判断存在,且为目录
对
[root@centos7~]# [ -d ver1.txt ] && echo对|| echo 错 #因为不是目录,结果错
错
下面这个测试,假设系统中有某个磁盘设备,使用-b测试该设备是否存在,且当该设备为块设备时返回值为真,否则返回值为假。
[root@centos7~]# [ -b /dev/sda ] && echo是|| echo 不是
是
[root@centos7~]# [ -b /etc/passwd ] && echo是|| echo 不是
不是
Linux系统中的文件链接分为软链接和硬链接两种。软链接创建后,如果源文件被删除,则软链接将无法继续使用,可以跨分区和磁盘创建软链接。硬链接创建后,如果源文件被删除,则硬链接依然可以正常使用、正常读写数据,但硬链接不可以跨分区或磁盘创建。另外,硬链接与源文件使用的是相同的设备、相同的inode编号。使用ls -l[插图]命令查看硬链接文件的属性时,文件属性与普通文件是一样的,而软链接的文件属性则可以看到被l标记,表示该文件为软链接。
[root@centos7~]# ln -s /etc/hosts /root/soft #创建软链接
[root@centos7~]# ln /etc/hosts /root/hard
#创建硬链接
[root@centos7~]# ls -l /root/soft
lrwxrwxrwx. 1 root root 10 9月 16 20:52 /root/soft -> /etc/hosts
[root@centos7~]# ls -l /root/hard
-rw-r--r--. 3 root root 158 9月 15 15:24 /root/hard
[root@centos7~]# [ -L /root/soft ] && echo是|| echo 不是 #判断是否为软链接
是
[root@centos7~]# [ ! -L /root/soft ] && echo是|| echo 不是 #判断不是软链接
错
[root@centos7~]# [ -L /root/hard ] && echo是|| echo 不是
不是
[root@centos7~]# [ /root/hard-ef /etc/hosts ] && echo Y || echo N
Y
在测试权限时需要注意,超级管理员root在没有rw权限的情况下,也是可以读写文件的,rw权限对超级管理员是无效的。但是如果文件没有x权限,哪怕是root也不可以执行该文件。
[root@centos7~]# ls -l ver1.txt
-rw-r--r--. 1 root root 0 9月 13 17:42 ver1.txt
[root@centos7~]# [ -r ver1.txt ] && echo Y || echo N
Y
[root@centos7~]# chmod -r ver1.txt #删除r权限
[root@centos7~]# [ -r ver1.txt ] && echo Y || echo N #测试结果依然为真
Y
[root@centos7~]# [ ! -r ver1.txt ] && echo Y || echo N #测试不可读
N
[root@centos7~]# chmod -w ver1.txt #删除w权限
[root@centos7~]# ls -l ver1.txt
----------. 1 root root 0 9月 16 20:31 ver1.txt
[root@centos7~]# [ -w ver1.txt ] && echo Y || echo N #测试结果依然为真
Y
[root@centos7~]# [ -x ver1.txt ] && echo Y || echo N #测试结果为假
N
[root@centos7~]# chmod +x ver1.txt #添加x权限
[root@centos7~]# ls -l ver1.txt
---x--x--x. 1 root root 0 9月 16 20:31 ver1.txt
[root@centos7~]# [ -x ver1.txt ] && echo Y || echo N
Y
默认touch命令创建的文件都是空文件,在使用-s测试文件是否为非空文件时,因为文件是空文件,所以测试结果为假。当文件中有内容时,测试文件是否为非空时,结果为真。
[root@centos7~]# [ -s ver1.txt ] && echo Y || echo N
N
[root@centos7~]# echo "hello" > ver1.txt
[root@centos7~]# [ -s ver1.txt ] && echo Y || echo N
Y
前面在创建ver1.txt和ver2.txt文件时,故意让两个文件创建的时间有所不同,现在可以使用测试条件判断两个文件的创建时间,看看哪个文件是新文件,哪个文件是旧文件。new than表示更新,old than表示更旧。根据下面的输出结果可知,ver2.txt文件比ver1.txt文件更新。
[root@centos7~]# ls -l ver*.txt
-rw-r--r--. 1 root root 0 9月 13 17:42 ver1.txt
-rw-r--r--. 1 root root 0 9月 13 17:43 ver2.txt
[root@centos7~]# [ ver1.txt -nt ver2.txt ] && echo Y || echo N
N
[root@centos7~]# [ ver2.txt -nt ver1.txt ] && echo Y || echo N
Y
[root@centos7~]# [ ver1.txt -ot ver2.txt ] && echo Y || echo N
Y
[root@centos7~]# [ ver2.txt -ot ver1.txt ] && echo Y || echo N
N