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

相关推荐
誰能久伴不乏16 分钟前
Linux `epoll` 学习笔记:从原理到正确写法(含 ET 经典坑总结)
linux·服务器·网络·c++·ubuntu
是阿威啊40 分钟前
【第一站】本地虚拟机部署Hadoop分布式集群
大数据·linux·hadoop·分布式
wdfk_prog1 小时前
[Linux]学习笔记系列 -- [fs]filesystems
linux·笔记·学习
fengyehongWorld1 小时前
Linux tree命令
linux
Percep_gan1 小时前
解决-bash: /usr/bin/yum: No such file or directory
开发语言·bash
像名字一样难取的昵称1 小时前
Linux学习笔记:十八、Linux文件的压缩,解压缩一站式学习
linux·运维·笔记·学习·ubuntu·ssh
少年、潜行1 小时前
F1C100/200S学习笔记(2)-- 初次FLASH启动(裸机)和SD卡启动(Linux)
linux·笔记·f1c200s
小白勇闯网安圈2 小时前
极简版Linux发行内核
linux
管理大亨2 小时前
Linux vs Windows:五大维度深度对决
linux·arm开发·windows
乘凉~2 小时前
【Linux作业】Limux下的python多线程爬虫程序设计
linux·爬虫·python