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?
This answer wrapped up as a shell script
Examples
is_dir
Using the
-e
check will check for files and this includes directories.Check if directory exists, else make one
If you want to check if a directory exists, regardless if it's a real directory or a symlink, use this:
Explanation: The "ls" command gives an error "ls: /x: No such file or directory" if the directory or symlink does not exist, and also sets the return code, which you can retrieve via "$?", to non-null (normally "1"). Be sure that you check the return code directly after calling "ls".
(1)
(2)
(3)
If found an issue with one of the approach provided above.
With
ls
command; the cases when directory does not exists - an error message is shown$ [[
ls -ld SAMPLE_DIR| grep ^d | wc -l
-eq 1 ]] && echo exists || not exists -ksh: not: not found [No such file or directory]