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

相关推荐
旖旎夜光7 分钟前
linux(8)(下)
linux·学习
谢平康33 分钟前
通过nfs方式做目录限额方法
linux·服务器·网络
刘一说34 分钟前
Windows 与 Linux 跨平台自动化 MySQL 8 备份:专业级脚本设计与实战指南
linux·数据库·windows·mysql·自动化
I · T · LUCKYBOOM36 分钟前
2.2yum安装--单服务器托管多网站
linux·运维·服务器
郝学胜-神的一滴39 分钟前
线程同步:并行世界的秩序守护者
java·linux·开发语言·c++·程序人生
Linux技术芯1 小时前
浅谈nvme协议版本的发展
linux
释怀不想释怀1 小时前
Linux(复习常见命令)
linux·运维·服务器
_F_y1 小时前
负载均衡在线oj(文件版)
linux·运维·负载均衡
Chase_______1 小时前
【Linux指南】:vi编辑器
linux·运维·编辑器
礼拜天没时间.1 小时前
【生产级实战】Linux 集群时间同步详解(NTP + Cron,超详细)
linux·运维·服务器·时间同步·cron·ntp