What command can be used to check if a directory exists or not, within a shell script?
Join Stack Overflow to learn, share knowledge, and build your career.
|
Have you considered just doing whatever you want to do in the IE, if you want to check for the existence of a directory before you enter it, try just doing this:
If the path you give to Seems better than this, which requires repeating yourself:
Same thing works with |
|||||
|
|
Git Bash + Dropbox + Windows : None of the other solutions worked for my Dropbox folder, which was weird because I can git push to dropbox symbolic path.
You'll probably want to know how to successfully navigate to dropbox from bash as well. So here is the script in it's entirity. |
|||
|
|
|
Note the -d test can produce some surprising results:
File under: "When is a directory not a directory?" The answer: "When it's a symlink to a directory." A slightly more thorough test:
You can find more information in the Bash manual on Bash conditional expressions and the |
|||||
|
|
||||
|
|
|
From Script file myScript.sh:
OR
|
|||
|
|
|
Remember to always wrap variables in double quotes when referencing them in a bash script. Kids these days grow up with the idea that they can have spaces and lots of other funny characters in their directory names. (Spaces! Back in my days, we didn't have no fancy spaces! ;)) One day, one of those kids will run your script with
|
|||||||||||||||||||||
|
|
This answer wrapped up as a shell script Examples
is_dir
|
||||
|
|
|
In kind of ternary form,
And with test
|
|||
|
|
|
Check if directory exists, else make one
|
|||||
|
|
You can use
For example:
Note: The
For possible options or further help, check:
|
||||
|
|
|
To check more than one directory use this code:
|
|||||
|
|
As per Jonathan comment: If you want to create the directory and it does not exist yet, then the simplest technique is to use
|
||||
|
|
|
To check if a directory exists you can use simple if structure like this:
You can do it also in negative
Note: Be careful, leave empty spaces on either side of both opening and closing braces. With the same syntax you can use:
|
||||
|
|
|
Shorter form:
|
|||||||||
|
|
|||||
|
|
(1)
(2)
(3)
If found an issue with one of the approach provided above. With $ [[ |
||||
|
|
|
Type this code on the bash promt
|
|||
|
|
More features using
|
|
|
Hi Niel. Your idea may be useful to check the existence of directories depending on a pattern like:
find -maxdepth 1 -type d -name 'pattern'. Do you mind if I append in your answer this trick? Cheers ;)
– olibre
Nov 18 '13 at 13:54
|
|
Below find can be used,
|
|||
|
|
|
The The following command in just one line will tell you if the given
Practical usage:
|
|||||
|
|
Here's a very pragmatic idiom:
I typically wrap it in a function:
Or:
The nice thing about this approach is that I do not have to think of a good error message.
Next is the argument that we pass to Tl;dr: If the string passed into this function is empty, we again exit from the subshell Quoting from the
and
The phrasing here is peculiar to the shell documentation, as In this particular case, I know that the standard error message A philosphical note:
The shell is not an object oriented language, so the message says |
|||||||||||||
|
|
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". |
|||||
|
|
Actually, you should use several tools to get a bulletproof approach:
No need to worry about spaces and special characters as long as you use Note that |
||||
|
|
|
||||
|
|
|
Using the
|
|||||
|
|
I find the double-bracket version of
|
|||||||||||||||||||||
|
|
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 (thanks Jon), 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. |
|||||||||||||||||||||
|
|
|||
|
|
|
Great solutions out there, but ultimately every script will fail if you're not in the right directory. So code like this:
will execute successfully only if at the moment of execution you're in a directory that has a subdirectory that you happen to check for. I understand the initial question like this: to verify if a directory exists irrespective of the user's position in the file system. So using the command 'find' might do the trick:
This solution is good because it allows the use of wildcards, a useful feature when searching for files/directories. The only problem is that, if the searched directory doesn't exist, the 'find' command will print nothing to stdout (not an elegant solution for my taste) and will have nonetheless a zero exit. Maybe someone could improve on this. |
|||||
|
The above code checks if the directory exists and if it is writable. |
|||||
|
protected by Community♦ Jan 5 '17 at 6:56
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?