linux文件测试语句

1、Linux系统中文件测试语句用于判断文件是否存在、是文件或者目录、或者权限判断

   linux系统中 echo $? 用于判断上一句是否成功执行,成功返回0,不成功返回其他数字,

   简单示例: 

复制代码
[root@linuxprobe test]# ls
[root@linuxprobe test]# mkdir test  ## 成功执行
[root@linuxprobe test]# echo $?  ## 返回0
0
[root@linuxprobe test]# mkdir test  ##未成功执行
mkdir: cannot create directory ‘test’: File exists
[root@linuxprobe test]# echo $?  ## 返回1
1
复制代码

 

2、格式 

   [ 条件表达式 ] ,条件表达式的两边一定要有空格

 

3、简单测试  -e判断文件或者目录是否存在 

复制代码
[root@linuxprobe test]# ls
a.txt  test
[root@linuxprobe test]# [ -e a.txt ]
[root@linuxprobe test]# echo $?  ## 存在返回0
0
[root@linuxprobe test]# [ -e b.txt ]  ## 不存在返回1
[root@linuxprobe test]# echo $?
1
[root@linuxprobe test]# [ -e test ]  ##同上
[root@linuxprobe test]# echo $?
0
[root@linuxprobe test]# [ -e test2 ]
[root@linuxprobe test]# echo $?
1
复制代码

 

4、-f判断是否为普通文件, -d 判断是否为目录

复制代码
[root@linuxprobe test]# touch a.txt  ## 普通文件
[root@linuxprobe test]# mkdir test  ##目录
[root@linuxprobe test]# ls -l
total 0
-rw-r--r--. 1 root root 0 Oct 14 06:53 a.txt
drwxr-xr-x. 2 root root 6 Oct 14 06:53 test
[root@linuxprobe test]# [ -f a.txt ] 
[root@linuxprobe test]# echo $?  ## 普通文件返回0
0
[root@linuxprobe test]# [ -f test ]
[root@linuxprobe test]# echo $?  ##目录,返回1
1
[root@linuxprobe test]# [ -d a.txt ] ## 普通文件,返回1
[root@linuxprobe test]# echo $?
1
[root@linuxprobe test]# [ -d test ] ##目录,返回0
[root@linuxprobe test]# echo $?
0
复制代码

 

5、-r 、-w、-x分别进行权限判断

复制代码
[root@linuxprobe test]# ls
[root@linuxprobe test]# touch a.txt b.txt
[root@linuxprobe test]# chmod 000 a.txt
[root@linuxprobe test]# chmod 777 b.txt
[root@linuxprobe test]# ll -h  ## 查看权限
total 0
----------. 1 root root 0 Oct 14 18:27 a.txt
-rwxrwxrwx. 1 root root 0 Oct 14 18:27 b.txt
[root@linuxprobe test]# whoami  ##root用户
root
[root@linuxprobe test]# [ -r a.txt ]   ## 对root无效?
[root@linuxprobe test]# echo $?
0
[root@linuxprobe test]# [ -w a.txt ]   ## 对root无效?
[root@linuxprobe test]# echo $?
0
[root@linuxprobe test]# [ -x a.txt ]  ## 执行权限可以
[root@linuxprobe test]# echo $?
1
[root@linuxprobe test]# su - linuxprobe  ## 切换至普通用户
[linuxprobe@linuxprobe ~]$ cd ../test/
[linuxprobe@linuxprobe test]$ ll -h
total 0
----------. 1 root root 0 Oct 14 18:27 a.txt
-rwxrwxrwx. 1 root root 0 Oct 14 18:27 b.txt
[linuxprobe@linuxprobe test]$ whoami
linuxprobe
[linuxprobe@linuxprobe test]$ [ -r a.txt ]  ## 对普通用户没有读的权限
[linuxprobe@linuxprobe test]$ echo $?
1
[linuxprobe@linuxprobe test]$ [ -w a.txt ]  ## 同上
[linuxprobe@linuxprobe test]$ echo $?
1
[linuxprobe@linuxprobe test]$ [ -x a.txt ]  ## 同上
[linuxprobe@linuxprobe test]$ echo $?
1
复制代码

 

复制代码
[linuxprobe@linuxprobe test]$ ls
a.txt  b.txt
[linuxprobe@linuxprobe test]$ whoami
linuxprobe
[linuxprobe@linuxprobe test]$ ll -h
total 0
----------. 1 root root 0 Oct 14 18:27 a.txt
-rwxrwxrwx. 1 root root 0 Oct 14 18:27 b.txt
[linuxprobe@linuxprobe test]$ [ -r b.txt ]
[linuxprobe@linuxprobe test]$ echo $?
0
[linuxprobe@linuxprobe test]$ [ -w b.txt ]
[linuxprobe@linuxprobe test]$ echo $?
0
[linuxprobe@linuxprobe test]$ [ -x b.txt ]
[linuxprobe@linuxprobe test]$ echo $?
0
[linuxprobe@linuxprobe test]$ su - root
Password:
[root@linuxprobe ~]# cd /home/test/
[root@linuxprobe test]# ll -h
total 0
----------. 1 root root 0 Oct 14 18:27 a.txt
-rwxrwxrwx. 1 root root 0 Oct 14 18:27 b.txt
[root@linuxprobe test]# whoami
root
[root@linuxprobe test]# [ -r b.txt ]
[root@linuxprobe test]# echo $?
0
[root@linuxprobe test]# [ -w b.txt ]
[root@linuxprobe test]# echo $?
0
[root@linuxprobe test]# [ -x b.txt ]
[root@linuxprobe test]# echo $?
0
复制代码

 

  

posted @   小鲨鱼2018  阅读(409)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示