What command can be used to check if a directory exists or not, within a shell script?
相关问题
- How to get the return code of a shell script in lu
- Is shmid returned by shmget() unique across proces
- Invoking Mirth Connect CLI with Powershell script
- Why should we check WIFEXITED after wait in order
- Emacs shell: save commit message
相关文章
- 使用2台跳板机的情况下如何使用scp传文件
- In IntelliJ IDEA, how can I create a key binding t
- shell中反引号 `` 赋值变量问题
- How get the time in milliseconds in FreeBSD?
- Making new files automatically executable?
- Reverse four length of letters with sed in unix
- Launch interactive SSH bash session from PHP
- What does it take to be durable on Linux?
To check if a directory exists in a shell script you can use the following:
Or to check if a directory doesn't exist:
However, as Jon Ericson points out, subsequent commands may not work as intended if you do not take into account that a symbolic link to a directory will also pass this check. E.g. running this:
Will produce the error message:
So symbolic links may have to be treated differently, if subsequent commands expect directories:
Take particular note of the double-quotes used to wrap the variables, the reason for this is explained by 8jean in another answer.
If the variables contain spaces or other unusual characters it will probably cause the script to fail.
I find the double-bracket version of
test
makes writing logic tests more natural:This is not completely true... If you want to go to that directory, you also needs to have the execute rights on the directory. Maybe you need to have write rights as well.
Therfore:
Or for something completely useless:
More features using
find
Check existence of the folder within sub-directories:
Check existence of one or several folders based on a pattern within the current directory:
Both combinations. In the following example, it checks the existence of the folder in the current directory: