Ste*_*ini 41
$ if test -d /the/dir; then echo "exist"; fi
Run Code Online (Sandbox Code Playgroud)
Bri*_*ter 14
假设你的 shell 是 BASH:
if [ -d /the/dir ]; then echo 'Exists'; else echo 'Not found'; fi
Run Code Online (Sandbox Code Playgroud)
规范的方法是使用test(1)实用程序:
test -d path && echo "Directory Exists"
Run Code Online (Sandbox Code Playgroud)
其中path是您要检查的目录的路径名。
例如:
test -d Desktop/ && echo "Directory Exists"
Directory Exists
test -d Desktop1/ && echo "Directory Exists"
# nothing appers
Run Code Online (Sandbox Code Playgroud)