文件检查
| 参数 | 意义 |
|---|---|
| -e | 该文件名是否存在 |
| -f | 该文件名是否存在且为file |
| -d | 该文件名是否存在且为目录 |
| -b | 该文件名是否存在且为一个block |
| -c | 该文件名是否存在且为一个character device设备 |
| -S | 该文件名是否存在且为一个socket文件 |
| -p | 该文件名是否存在且为一个FIFO(pipe)文件 |
| -L | 该文件名是否存在且为一个连接文件 |
文件权限检查
| **参数 | 意义** |
|---|---|
| -r | 检测文件名是否存在且具有“可读”权限 |
| -w | 检测文件名是否存在且具有“可写”权限 |
| -x | 检测文件名是否存在且具有“可执行”权限 |
| -u | 检测文件名是否存在且具有“SUID”权限 |
| -g | 检测文件名是否存在且具有“SGID”权限 |
| -k | 检测文件名是否存在且具有“Sticky bit”权限 |
| -s | 检测文件名是否存在且为“非空白文件” |
文件的比较
| 参数 | 意义 |
|---|---|
| -nt | (newer than)判断file1是否比file2新 |
| -ot | (older than)判断file是否比file2旧 |
| -ef | 判断file1与file2是否为同一个文件,可用在判断hard link上 |
数字之间的判定
| 参数 | 意义 |
|---|---|
| -eq | equal |
| -ne | not equal |
| -gt | greater than |
| -lt | less than |
| -ge | greater than or equal |
| -le | less than or equal |
判断字符串的数据
| 参数 | 意义 |
|---|---|
| test -z string | 判断字符串是否为0,若为空返回true |
| test -n string | 判断字符串是否为0,若为空返回false |
| test str1 = str2 | 判断str1是否等于str2,若相等,返回true |
| test str1 != str2 | 判断str1是否等于str2,若相等,返回false |
多重条件的判断
| 参数 | 意义 |
|---|---|
| -a | 两个条件同时成立,eg:test -r file -a -x file,同时成立返回true |
| -o | 任何一个条件成立,eg:test -r file -o -x file,同时成立返回true |
| ! | 反向状态,如test!-x file,当file不具有x时,返回true |
通常我们会使用[ ]代替test,
注意:[ -f file ]与中间的判断条件需要用空格隔开

[root@bogon /home/Sudley]#ls |grep -v '[[:digit:]]'
a.out
dd
filename
node.h
test.c
testfile
test.sh
testup.c
[root@bogon /home/Sudley]#sh test.sh
test is true,there is test.c
[] is true,there is test.c
test is true,there is test.c,number=120
test is true,there is test.c,number=120
本文详细介绍如何使用命令行工具检查文件属性,包括文件类型、权限、比较及字符串判断等,适用于Linux系统管理员和脚本开发者。
738

被折叠的 条评论
为什么被折叠?



