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

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

    As per Jonathan's comment:

    If you want to create the directory and it does not exist yet, then the simplest technique is to use mkdir -p which creates the directory — and any missing directories up the path — and does not fail if the directory already exists, so you can do it all at once with:

    mkdir -p /some/directory/you/want/to/exist || exit 1
    

提交回复
热议问题