Linux 基础教程

Linux 参考手册

Linux 笔记

original icon
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.knowledgedict.com/tutorial/linux-check-if-a-file-exists-in-shell.html

shell 脚本判断文件是否存在

Linux Shell 脚本编程详解 Linux Shell 脚本编程详解


linux shell 脚本判断文件是否存在,主要是利用 test 表达式的,具体参考如下。

判断方法

其实有很多 test 表达式都有判断文件是否存在的,常用 -e-f,具体如下:

#! /bash/sh
check_file='/run.log'
if [ ! -f $check_file ]; then
  echo "$check_file no exist"
else
  echo "exist"
fi

test 表达式 -f 表示文件是否是普通文件,也可以将其替换为 -e-e 只判断文件是否存在。