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

后端 未结 30 2293
猫巷女王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:30

    You can use test -d (see man test).

    -d file True if file exists and is a directory.

    For example:

    test -d "/etc" && echo Exists || echo Does not exist
    

    Note: The test command is same as conditional expression [ (see: man [), so it's portable across shell scripts.

    [ - This is a synonym for the test builtin, but the last argument must, be a literal ], to match the opening [.

    For possible options or further help, check:

    • help [
    • help test
    • man test or man [

提交回复
热议问题