Use the shell to determine if a folder or file exists
Determine if a folder exists
if [! -d "/etc/open" ];thenecho "文件夹/etc/open不存在"esleecho "文件夹/etc/open存在"fi
Determine if the file exists and delete it if it exists
if [ ! -f "/etc/filename" ];thenecho "文件不存在"elserm -rf /etc/lenameecho "filename文件已删除"fi
Determine if a folder exists
if [ -d "/data/" ];thenecho "文件夹存在"elseecho "文件夹不存在"fi
Determine if a file exists
if [ -f "/data/filename" ];thenecho "文件存在"elseecho "文件不存在"fi
- Comparison characters
-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" ]
Use the shell to determine if a folder or file exists