How can I check if a directory exists in a Bash shell script?

后端 未结 30 2303
猫巷女王i
猫巷女王i 2020-11-22 10:35

What command can be used to check if a directory exists or not, within a Bash shell script?

30条回答
  •  一整个雨季
    2020-11-22 11:34

    [[ -d "$DIR" && ! -L "$DIR" ]] && echo "It's a directory and not a symbolic link"
    

    N.B: Quoting variables is a good practice.

    Explanation:

    • -d: check if it's a directory
    • -L: check if it's a symbolic link

提交回复
热议问题