什么命令检查Bash shell脚本中是否存在目录?
当前回答
检查目录是否存在,否则创建一个:
[ -d "$DIRECTORY" ] || mkdir $DIRECTORY
其他回答
可以使用以下发现,
find . -type d -name dirname -prune -print
file="foo"
if [[ -e "$file" ]]; then echo "File Exists"; fi;
或者对于完全无用的东西:
[ -d . ] || echo "No"
较短形式:
# if $DIR is a directory, then print yes
[ -d "$DIR" ] && echo "Yes"
如果要检查目录是否存在,无论它是真实目录还是符号链接,请使用以下命令:
ls $DIR
if [ $? != 0 ]; then
echo "Directory $DIR already exists!"
exit 1;
fi
echo "Directory $DIR does not exist..."
解释:如果目录或符号链接不存在,则“ls”命令会给出错误“ls:/x:No such file or directory”,并将返回代码设置为非空(通常为“1”),您可以通过“$?”检索该代码。请确保在调用“ls”后直接检查返回代码。