shell 判断文件是否存在(csh bash)

文章目录

    • 前言
      • [1. -e 判断文件是否存在](#1. -e 判断文件是否存在)
      • [2. -f 判断文件是否存在且为普通文件](#2. -f 判断文件是否存在且为普通文件)
      • [3. -d 判断文件是否存在且为目录](#3. -d 判断文件是否存在且为目录)
      • [3. -s 判断文件是否存在且不为空](#3. -s 判断文件是否存在且不为空)
      • [4. -r 判断文件是否存在且可读](#4. -r 判断文件是否存在且可读)
      • [5. -w 判断文件是否存在且可写](#5. -w 判断文件是否存在且可写)
      • [6. -x 判断文件是否存在且可执行](#6. -x 判断文件是否存在且可执行)

前言

Shell 编程能提升我们工作效率,在 shell 中, 可以借助文件测试符号来判断一个文件是否存在。 常用的文件测试符号有 -e, -f, -d, -s, -r。

1. -e 判断文件是否存在

bash 复制代码
#!/bin/csh

set fileName = "test.txt"
#set fileName = "test_dir"

if (-e $fileName) then
  echo "$fileName" " is exit"
else
  echo "$fileName" " is not exit"
endif
bash 复制代码
#!/bin/bash

file=test.txt
#file=test_dir

if [ -e "$file" ]; then
  echo "$file" "is exit"
else
  echo "$file" "is not exit"
fi

2. -f 判断文件是否存在且为普通文件

3. -d 判断文件是否存在且为目录

bash 复制代码
#!/bin/csh

set fileName = "test_test"

if (-d $fileName) then
  echo "$fileName" " is exit"
else
  echo "$fileName" " is not exit"
endif

3. -s 判断文件是否存在且不为空

4. -r 判断文件是否存在且可读

5. -w 判断文件是否存在且可写

6. -x 判断文件是否存在且可执行

相关推荐
q***13342 小时前
Linux(CentOS)安装 Nginx
linux·nginx·centos
摘星编程2 小时前
openGauss 快速上手:CentOS 环境下单机部署完整指南
linux·运维·centos
哈里谢顿7 小时前
使用kvm创建一台虚拟机
linux
hanyi_qwe8 小时前
文本三剑客--awk
linux·运维·服务器
Caven778 小时前
【Linux 技巧】如何在登录时自动激活 Conda Base 环境
linux·运维·conda
凌寒118 小时前
Linux(Debian)安装、卸载 MySQL
linux·运维·mysql·debian
IT小哥哥呀8 小时前
如何从 Windows SSH 进入 VirtualBox Ubuntu 虚拟机——密码认证(逐步指南)
linux·windows·ubuntu·ssh·教程·虚拟机
怀旧,9 小时前
【Linux系统编程】7. 进程的概念(上)
linux·运维·服务器
7***n7510 小时前
API网关设计模式
linux·服务器·设计模式
哈里谢顿10 小时前
环境变量 HTTP_PROXY/HTTPS_PROXY 深度解析:为什么 cURL 行,Docker 不行?
linux