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 判断文件是否存在且可执行

相关推荐
4t4run14 小时前
25、Linux 特殊权限
linux·运维
氵文大师15 小时前
A机通过 python -m http.server 下载B机的文件
linux·开发语言·python·http
HUT_Tyne26515 小时前
Linux 快速入门
linux·运维·服务器
leoufung16 小时前
逆波兰表达式 LeetCode 题解及相关思路笔记
linux·笔记·leetcode
鸠摩智首席音效师16 小时前
如何在 Linux 中使用 dd 命令 ?
linux·运维·服务器
一夜空中最亮的星一16 小时前
【Linux】ubuntu24.04 安装docker
linux·docker·eureka
赖small强16 小时前
【Linux 网络基础】libwebsockets 技术文档
linux·网络·https·tls·lib·websockets
q***518917 小时前
ubuntu 安装 Redis
linux·redis·ubuntu
q***471818 小时前
使用Canal将MySQL数据同步到ES(Linux)
linux·mysql·elasticsearch
Y淑滢潇潇18 小时前
RHCE 防火墙实验
linux·运维·rhce