Shell judgment judgment of files and folders

File and file object parameters:

-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" ]

Determine whether the file exists

if [ -f "/data/filename" ];then
  echo "文件存在"
else
  echo "文件不存在"
fi

Determine whether the folder exists

if [ -d "/data/" ];then
  echo "文件夹存在"
else
  echo "文件夹不存在"
fi

Determine whether the file or folder size is greater than 0

if [ -s "/data/" ];then
  echo "文件夹或文件大小大于0"
else
  echo "文件夹或文件大不小大于0"
fi

Determine whether file1 is newer than file2

if [ "/data/file1" -nt "/data/file2" ];then
  echo "file1比file2新"
else
  echo "file1比file2旧"
fi

Guess you like

Origin blog.csdn.net/lz6363/article/details/115035302