shell script - to determine whether there is a folder or file

Reference https://www.cnblogs.com/37yan/p/6962563.html

Folder does not exist, create

if [ ! -d "/data/" ];then
mkdir /data
else
echo "文件夹已经存在"
fi

Delete the file exists

if [ ! -f "/data/filename" ];then
echo "文件不存在"
else
rm -f /data/filename
fi
Determine whether there is a folder
if [ -d "/data/" ];then
echo "文件夹存在"
else
echo "文件夹不存在"
fi
Determine whether a file exists
if [ -f "/data/filename" ];then
echo "文件存在"
else
echo "文件不存在"
fi

File comparison operators

-e 判断对象是否存在
-d 判断对象是否存在,并且为目录
-f 判断对象是否存在,并且为常规文件
-L 判断对象是否存在,并且为符号链接
-h 判断对象是否存在,并且为软链接
-s 判断对象是否存在,并且长度不为0
-r 判断对象是否存在,并且可读
-w 判断对象是否存在,并且可写
-x 判断对象是否存在,并且可执行
-O 判断对象是否存在,并且属于当前用户
-G 判断对象是否存在,并且属于当前用户组
-nt 判断file1是否比file2新  [ "/data/file1" -nt "/data/file2" ]
-ot 判断file1是否比file2旧  [ "/data/file1" -ot "/data/file2" ]

Reproduced in: https: //www.jianshu.com/p/5aea40242281

Guess you like

Origin blog.csdn.net/weixin_34414650/article/details/91096144