What command checks if a directory exists or not within a Bash shell script?
35 Answers
To check if a directory exists:
if [ -d "$DIRECTORY" ]; then
echo "$DIRECTORY does exist."
fi
To check if a directory does not exist:
if [ ! -d "$DIRECTORY" ]; then
echo "$DIRECTORY does not exist."
fi
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:
ln -s "$ACTUAL_DIR" "$SYMLINK"
if [ -d "$SYMLINK" ]; then
rmdir "$SYMLINK"
fi
Will produce the error message:
rmdir: failed to remove `symlink': Not a directory
So symbolic links may have to be treated differently, if subsequent commands expect directories:
if [ -d "$LINK_OR_DIR" ]; then
if [ -L "$LINK_OR_DIR" ]; then
# It is a symlink!
# Symbolic link specific commands go here.
rm "$LINK_OR_DIR"
else
# It's a directory!
# Directory command goes here.
rmdir "$LINK_OR_DIR"
fi
fi
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.

- 24,552
- 19
- 101
- 135

- 124,925
- 25
- 94
- 111
-
40If you want to play it safe with the GNU tools, use of `--` is highly recommended (end-of-options marker). Otherwise, if your variable contains something that looks like an option, the script'll fail just as with spaces. – Marc Mutz - mmutz Jul 21 '09 at 16:36
-
4For modern versions of bash, ksh, etc. [...] is a builtin – fpmurphy Mar 24 '11 at 14:22
-
96One thing to keep in mind: `[ ! -d "$DIRECTORY" ]` will be true either if `$DIRECTORY` doesn't exist, or if *does* exist but isn't a directory. Consider something like `if [ ! -d "$DIRECTORY" ] ; then mkdir "$DIRECTORY" ; fi`; this will fail if `"$DIRECTORY"` is a file. (Of course you should check whether `mkdir` succeeded anyway; there are a number of reasons it can fail.) – Keith Thompson Aug 09 '11 at 23:46
-
Minor note. In bash, help [, man test (as already noted) and info test (on "Not UNIXes") provide documentation. – Rob Kielty Sep 26 '12 at 12:52
-
2should I use `[ ]` or `[[ ]]`? – Jürgen Paul Jul 26 '13 at 03:45
-
9It might be worth mentioning that as soon as the check has been performed the situation can have changed already due to other processes. In many cases it is better to just create or use the directory and react on a failure. – Alfe Sep 09 '13 at 11:51
-
For some reason, I'm getting an error with this: ``if [ -d "$TARGET_DIR/$DATE1" ]; then $RSYNC_PARAMS="$RSYNC_PARAMS --link-dest=$TARGET_DIR/$DATE1" fi`` – BlueCacti Nov 15 '13 at 16:02
-
``DATE1='date -I -d "1 day ago"'`` ; ``TARGET_DIR="/backup"`` which is a link; ``RSYNC_PARAMS="-azrtplH"`` – BlueCacti Nov 15 '13 at 16:09
-
18Instead of testing for both the directory (`-d`) and the symlink (`-L`), it's easier just to append a slash to the variable, like `if [ -d "${THING:+$THING/}" ]`. A directory won't mind the extra slash. A file will evaluate to false. Empty will remain empty, so false. And a symlink will be resolved to its destination. Of course, it depends on your goal. If you want to *go* there, this is fine. If you want to *delete it*, then the code in this answer is better. – ghoti Jan 17 '17 at 09:21
-
Using test does NOT work with an unplugged usb stick, since the journaling FS pretends the directory is still there if you already checked it before. I found that in this case checking the exit code of ls was the only way to check reliably if the directory exists: `if ls "$DIRECTORY"; then echo "dir exists" ; fi` – henon Oct 03 '17 at 09:41
-
Why doesn't this work for relative paths? For example `../../../../../my_dir`. – Danijel Jan 10 '19 at 10:40
-
Is there any difference between `-L` and `-h`? This website https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Bash-Conditional-Expressions says they're both `True if file exists and is a symbolic link.` So what's the difference? – Shayan Apr 05 '20 at 17:41
-
The spaces within `[` and `]`, around the `-d` or `! -d` are important, otherwise script will fail with command not found. – William Turrell Apr 05 '20 at 21:11
-
On the vein of teach a man to fish I rarely see anyone suggesting checking `man test` in the comments. Since it describes each test option I would suggest reviewing it as a fast way to determine comparisons you can perform. – Illuminatus Sep 16 '21 at 21:25
Always wrap variables in double quotes when referencing them in a Bash script.
if [ -d "$DIRECTORY" ]; then
# Will enter here if $DIRECTORY exists, even if it contains spaces
fi
Kids these days put spaces and lots of other funny characters in their directory names. (Spaces! Back in my day, we didn't have no fancy spaces!)
One day, one of those kids will run your script with $DIRECTORY
set to "My M0viez"
and your script will blow up. You don't want that. So use double quotes.

- 24,552
- 19
- 101
- 135

- 8,872
- 2
- 22
- 13
-
17Another reason to use double quotes is in case $DIRECTORY is not set for some reason. – Jon 'links in bio' Ericson Sep 15 '08 at 22:41
-
5"always wrap variables in double quotes...in a bash script." For bash, not technically necessary when using [[...]]; see http://tldp.org/LDP/abs/html/testconstructs.html#DBLBRACKETS (note: no word splitting): "No filename expansion or word splitting takes place between [[ and ]], but there is parameter expansion and command substitution." – michael Sep 12 '14 at 01:31
-
4Directories on Unix/Linux should not have any whitespaces, and subsequently scripts should not be adapted to it. It's bad enough Windows supports it, with all consequences to Windows scripting, but please, for the love of whatever, no need to introduce unnecessary requirements. – tvCa Dec 24 '14 at 13:57
-
27@tvCa I find that users generally prefer to be allowed more flexibility in their directory names rather than being forced to make things easier for developers. (In fact, when dealing with long file names, I find ones without spaces to be a pain as that kills word wrapping even though I myself have suffered in the past from not accounting for paths with spaces in scripts and programs.) – JAB Aug 12 '15 at 15:52
-
@JAB I agree, but then again such long directory names cry out for making multiple directories and better sorting / categorizing / organizing files into those. – Zelphir Kaltstahl May 06 '16 at 14:12
-
1This is really a separate question; http://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-variable – tripleee Jun 09 '16 at 07:39
-
6Ha. Spaces are just characters that have no glyphs usually. Anyway, you can escape them with a backslash. – uchuugaka Oct 18 '16 at 08:27
-
1@tvCa Many scripts which were happy in standard *nix environments suddenly find themselves in Windows environments (think cygwin, or git for Windows/Msys2). Or they just are leat loose on Samba shares. It is no coincidence that more and more tools get the option to produce or process NUL terminated file names. – Peter - Reinstate Monica Oct 11 '17 at 07:40
-
Another advantage of quoting variables: performance - Bash does not need to attempt word splitting on the contents of the quoted string. (Mostly it is negligable) – Gert van den Berg Apr 18 '18 at 13:22
-
`IFS="\t\n"` helps a lot in shell scripts when dealing with spaces in filenames/dirnames (where \t is an actual tab character and the \n is an actual LF, so this occupies two lines in the script :) – tzot Sep 04 '18 at 15:59
-
-
3Spaces in filenames are evil, I'm currently on a mission, with Wowbagger the Infinitely Prolonged, to insult all those who use them :-) – paxdiablo Mar 09 '20 at 23:46
-
Spaces in a filename, fine, exclamation marks and ampersands, no problem, but why oh why would anyone feel the need to put a newline in a filename?! – mattst Nov 07 '21 at 11:12
-
Unix allowed all characters except NUL and `/` in a file or directory name from the start. That was decades before VFAT arrived. – David Ongaro Aug 30 '22 at 06:09
Note the -d test can produce some surprising results:
$ ln -s tmp/ t
$ if [ -d t ]; then rmdir t; fi
rmdir: directory "t": Path component not a directory
File under: "When is a directory not a directory?" The answer: "When it's a symlink to a directory." A slightly more thorough test:
if [ -d t ]; then
if [ -L t ]; then
rm t
else
rmdir t
fi
fi
You can find more information in the Bash manual on Bash conditional expressions and the [
builtin command and the [[
compound commmand.

- 730,956
- 141
- 904
- 1,278

- 20,880
- 12
- 98
- 148
-
15or, assuming it is only necessary to work on directories (and links can be ignored) => `if [ -d tmpdir -a ! -L tmpdir ]; then echo "is directory"; rmdir tmpdir; fi` ... or, for one command that works on both links & dirs: `rm -r tmpdir` – michael Sep 12 '14 at 01:47
I find the double-bracket version of test
makes writing logic tests more natural:
if [[ -d "${DIRECTORY}" && ! -L "${DIRECTORY}" ]] ; then
echo "It's a bona-fide directory"
fi
-
-
18@TheVillageIdiot and @Hedgehog, are you using bash shell? The double bracket isn't universally supported. Here's a SO answer on that point: http://stackoverflow.com/questions/669452/is-preferable-over-in-bash-scripts/669486#669486 – yukondude Jul 02 '11 at 14:54
-
9And in Busybox ash with default compilation options `[[ ]]` is supported, but doesn't in fact provide any different functionality to `[ ]`. If portability is a concern, stick with `[ ]` and use the necessary workarounds. – dubiousjim May 31 '12 at 15:34
-
9...if using bash constructs in a shell script, the first line of the script should be: #!/bin/bash (and not #!/bin/sh, ksh, etc) – michael Sep 12 '14 at 01:23
-
5When using double square brackets in bash, you do not need to quote the variables. – Hubert Grzeskowiak Aug 11 '17 at 05:58
-
The portable Posix version of this answer is `if [ -d "$DIRECTORY" ] && ! [ -h "$DIRECTORY ]; then …` (`-L` is deprecated, `-h` is its replacement. Use `-L` if you're on a really ancient Unix system). – Adam Katz Apr 13 '20 at 17:05
-
@AdamKatz: `-L` is deprecated? In what system? Neither `help test` in Bash nor the GNU coreutils man page or info page mentions this. – Soren Bjornstad Jan 10 '21 at 17:59
-
@SorenBjornstad – deprecated isn't the right word, but the man page for `dash` says "This operator is retained for compatibility with previous versions of this program. Do not rely on its existence; use `-h` instead." – Adam Katz Jan 11 '21 at 00:48
-
@AdamKatz Portability and deprecation of features in dash don't matter since this is about bash. – konsolebox May 22 '21 at 20:09
Shorter form:
# if $DIR is a directory, then print yes
[ -d "$DIR" ] && echo "Yes"
-
10Does this work like this: `if $dir is a dir, then echo "yes"`? A bit of explanation would help :) – Martijn Aug 05 '16 at 07:38
-
7`cmd && other` is a common shorthand for `if cmd; then other; fi` -- this works with most programming languages which support Boolean logic, and is known as [short-circuit evaluation](https://en.wikipedia.org/wiki/Short-circuit_evaluation). – tripleee Oct 05 '16 at 04:03
-
7The behavior is not the same under `set -e` (which is a [shell programming best practice](https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/)). – dolmen Jul 23 '18 at 12:40
-
3@dolmen the `[ -d "$DIR" ]` is checked (followed by `&& echo Yes`), so I believe `set -e` makes no difference to the script behaviour (i.e if the test fails, the script continues normally). – tzot Sep 04 '18 at 16:03
-
@dolmen That's not correct. The behaviour is exactly the same under `set -e`. Except that if _echo_ fails, the script will exit. `[ -d "$DIR" ]` alone (no `&& cmd ...`) will cause the script to exit under `set -e`, if the test fails (ie. the directory does not exist, or `$DIR` exists but is not a directory). – dan Jul 18 '22 at 10:40
A simple script to test if a directory or file is present or not:
if [ -d /home/ram/dir ] # For file "if [ -f /home/rama/file ]" then echo "dir present" else echo "dir not present" fi
A simple script to check whether the directory is present or not:
mkdir tempdir # If you want to check file use touch instead of mkdir ret=$? if [ "$ret" == "0" ] then echo "dir present" else echo "dir not present" fi
The above scripts will check if the directory is present or not
$?
if the last command is a success it returns "0", else a non-zero value. Supposetempdir
is already present. Thenmkdir tempdir
will give an error like below:mkdir: cannot create directory ‘tempdir’: File exists

- 36,089
- 7
- 43
- 59

- 5,145
- 4
- 26
- 36
-
8The second one would create the directory, if it didn't exist at first. Then it's not idempotent. – Shihe Zhang Mar 06 '19 at 06:47
-
2. The 2nd one seems dangerous. Since it creates the directory, it's not even true anymore that the dir is not present. – Chris Feb 07 '20 at 21:38
-
mkdir will also not create the full path (without a param), but the opposite would be even more dangerous, as you are not even able to revert (rm -f) the changes as you don't know which directories it created – venimus Mar 23 '21 at 23:19
To check if a directory exists you can use a simple if
structure like this:
if [ -d directory/path to a directory ] ; then
# Things to do
else #if needed #also: elif [new condition]
# Things to do
fi
You can also do it in the negative:
if [ ! -d directory/path to a directory ] ; then
# Things to do when not an existing directory
Note: Be careful. Leave empty spaces on either side of both opening and closing braces.
With the same syntax you can use:
-e: any kind of archive
-f: file
-h: symbolic link
-r: readable file
-w: writable file
-x: executable file
-s: file size greater than zero

- 30,738
- 21
- 105
- 131

- 1,878
- 1
- 13
- 14
-
How is this any better than the accepted answer from 2008, aside from going off-topic with the file switches? – Dan Dascalescu Mar 05 '20 at 13:14
-
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 thetest
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
orman [

- 155,785
- 88
- 678
- 743
Or for something completely useless:
[ -d . ] || echo "No"

- 222,467
- 53
- 283
- 367
-
9It will never print "No". Current directory always exists, unless deleted by another thread or other ways. – Jahid Apr 19 '15 at 00:49
Here's a very pragmatic idiom:
(cd $dir) || return # Is this a directory,
# and do we have access?
I typically wrap it in a function:
can_use_as_dir() {
(cd ${1:?pathname expected}) || return
}
Or:
assert_dir_access() {
(cd ${1:?pathname expected}) || exit
}
The nice thing about this approach is that I do not have to think of a good error message.
cd
will give me a standard one line message to standard error already. It will also give more information than I will be able to provide. By performing the cd
inside a subshell ( ... )
, the command does not affect the current directory of the caller. If the directory exists, this subshell and the function are just a no-op.
Next is the argument that we pass to cd
: ${1:?pathname expected}
. This is a more elaborate form of parameter substitution which is explained in more detail below.
Tl;dr: If the string passed into this function is empty, we again exit from the subshell ( ... )
and return from the function with the given error message.
Quoting from the ksh93
man page:
${parameter:?word}
If
parameter
is set and is non-null then substitute its value; otherwise, printword
and exit from the shell (if not interactive). Ifword
is omitted then a standard message is printed.
and
If the colon
:
is omitted from the above expressions, then the shell only checks whether parameter is set or not.
The phrasing here is peculiar to the shell documentation, as word
may refer to any reasonable string, including whitespace.
In this particular case, I know that the standard error message 1: parameter not set
is not sufficient, so I zoom in on the type of value that we expect here - the pathname
of a directory.
A philosophical note:
The shell is not an object oriented language, so the message says pathname
, not directory
. At this level, I'd rather keep it simple - the arguments to a function are just strings.

- 30,738
- 21
- 105
- 131

- 8,088
- 1
- 43
- 57
-
This do more than only check for existance: This check for accessibility at your user level. SO question stand for *existance* only. So right answer is `test -d` as @Grundlefleck explained. – F. Hauri - Give Up GitHub Feb 09 '13 at 20:16
-
8@F.Hauri - He didn't ask for anything more, that's true. However, I've found that I typically need to know more than that. – Henk Langeveld Feb 09 '13 at 23:50
-
And it never occurred to me that no test can be conclusive, unless it runs as root. `test -d /unreadable/exists` will fail, even if the argument exists. – Henk Langeveld Apr 02 '16 at 17:32
if [ -d "$Directory" -a -w "$Directory" ]
then
#Statements
fi
The above code checks if the directory exists and if it is writable.

- 30,738
- 21
- 105
- 131

- 429
- 4
- 2
-
1-a is identical in effect to -e. It has been "deprecated," and its use is discouraged. – CousinCocaine Jan 28 '14 at 08:39
More features using find
Check existence of the folder within sub-directories:
found=`find -type d -name "myDirectory"` if [ -n "$found" ] then # The variable 'found' contains the full path where "myDirectory" is. # It may contain several lines if there are several folders named "myDirectory". fi
Check existence of one or several folders based on a pattern within the current directory:
found=`find -maxdepth 1 -type d -name "my*"` if [ -n "$found" ] then # The variable 'found' contains the full path where folders "my*" have been found. fi
Both combinations. In the following example, it checks the existence of the folder in the current directory:
found=`find -maxdepth 1 -type d -name "myDirectory"` if [ -n "$found" ] then # The variable 'found' is not empty => "myDirectory"` exists. fi

- 14,470
- 2
- 37
- 47

- 2,116
- 16
- 21
-
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 ;) – oHo Nov 18 '13 at 13:54
DIRECTORY=/tmp
if [ -d "$DIRECTORY" ]; then
echo "Exists"
fi

- 19,879
- 23
- 80
- 93
-
remeber `space` after `[` -> **[` `-d**. i got error because of missing space – Raj Feb 20 '20 at 06:00
-
4[AGAIN](https://stackoverflow.com/questions/59838/how-to-check-if-a-directory-exists-in-a-bash-shell-script#comment107113517_23077923), this answer was already given in 2008, with more useful explanations. The only new thing here is the online playground. – Dan Dascalescu Mar 05 '20 at 13:16
Actually, you should use several tools to get a bulletproof approach:
DIR_PATH=`readlink -f "${the_stuff_you_test}"` # Get rid of symlinks and get abs path
if [[ -d "${DIR_PATH}" ]] ; Then # Now you're testing
echo "It's a dir";
fi
There isn't any need to worry about spaces and special characters as long as you use "${}"
.
Note that [[]]
is not as portable as []
, but since most people work with modern versions of Bash (since after all, most people don't even work with command line :-p), the benefit is greater than the trouble.

- 30,738
- 21
- 105
- 131

- 578,959
- 113
- 301
- 329
Have you considered just doing whatever you want to do in the if
rather than looking before you leap?
I.e., if you want to check for the existence of a directory before you enter it, try just doing this:
if pushd /path/you/want/to/enter; then
# Commands you want to run in this directory
popd
fi
If the path you give to pushd
exists, you'll enter it and it'll exit with 0
, which means the then
portion of the statement will execute. If it doesn't exist, nothing will happen (other than some output saying the directory doesn't exist, which is probably a helpful side-effect anyways for debugging).
It seems better than this, which requires repeating yourself:
if [ -d /path/you/want/to/enter ]; then
pushd /path/you/want/to/enter
# Commands you want to run in this directory
popd
fi
The same thing works with cd
, mv
, rm
, etc... if you try them on files that don't exist, they'll exit with an error and print a message saying it doesn't exist, and your then
block will be skipped. If you try them on files that do exist, the command will execute and exit with a status of 0
, allowing your then
block to execute.

- 30,738
- 21
- 105
- 131

- 20,617
- 19
- 137
- 193
-
2pushd is to me the most elegant way of doing this. I was about to post it as an answer :) – melMass Dec 07 '17 at 17:10
[[ -d "$DIR" && ! -L "$DIR" ]] && echo "It's a directory and not a symbolic link"
N.B: Quoting variables is a good practice.
Explanation:
-d
: check if it's a directory-L
: check if it's a symbolic link

- 21,542
- 10
- 90
- 108
-
An explanation would be in order (by [editing your answer](https://stackoverflow.com/posts/15015952/edit), not here in comments). – Peter Mortensen May 07 '20 at 12:14
-
Quoting variables (even with spaces with paths and metacharacters) is mostly not necessary if you're using Bash's `[[...]]`. The one exception is the RHS of `=`, e.g. `[[ $VAR = "$VAR" ]]`. (This is due to Bash interpreting the RHS as a glob.) – amphetamachine Jul 08 '22 at 16:27
Check if the directory exists, else make one:
[ -d "$DIRECTORY" ] || mkdir $DIRECTORY

- 30,738
- 21
- 105
- 131

- 7,352
- 2
- 36
- 29
-
20
-
Need double quotes around `$DIRECTORY` in the `mkdir` part as well. Otherwise, word splitting may result in undesirable results. For example: `dir="a b"; mkdir $dir` will result in two directories `a` and `b` being created, rather than a single `a b` directory. – codeforester Feb 22 '21 at 03:55
[ -d ~/Desktop/TEMPORAL/ ] && echo "DIRECTORY EXISTS" || echo "DIRECTORY DOES NOT EXIST"

- 8,048
- 1
- 32
- 48

- 1,124
- 13
- 12
-
An explanation would be in order (by [editing your answer](https://stackoverflow.com/posts/15015952/edit), not here in comments). – Peter Mortensen May 07 '20 at 12:07
To check more than one directory use this code:
if [ -d "$DIRECTORY1" ] && [ -d "$DIRECTORY2" ] then
# Things to do
fi

- 1,087
- 2
- 17
- 24

- 769
- 1
- 7
- 12
Using the -e
check will check for files and this includes directories.
if [ -e ${FILE_PATH_AND_NAME} ]
then
echo "The file or directory exists."
fi

- 8,048
- 1
- 32
- 48

- 281
- 3
- 5
This answer wrapped up as a shell script
Examples
$ is_dir ~
YES
$ is_dir /tmp
YES
$ is_dir ~/bin
YES
$ mkdir '/tmp/test me'
$ is_dir '/tmp/test me'
YES
$ is_dir /asdf/asdf
NO
# Example of calling it in another script
DIR=~/mydata
if [ $(is_dir $DIR) == "NO" ]
then
echo "Folder doesnt exist: $DIR";
exit;
fi
is_dir
function show_help()
{
IT=$(CAT <<EOF
usage: DIR
output: YES or NO, depending on whether or not the directory exists.
)
echo "$IT"
exit
}
if [ "$1" == "help" ]
then
show_help
fi
if [ -z "$1" ]
then
show_help
fi
DIR=$1
if [ -d $DIR ]; then
echo "YES";
exit;
fi
echo "NO";

- 1
- 1

- 66,836
- 64
- 257
- 336
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

- 30,738
- 21
- 105
- 131

- 155,785
- 88
- 678
- 743
if [ -d "$DIRECTORY" ]; then
# Will enter here if $DIRECTORY exists
fi
This is not completely true...
If you want to go to that directory, you also need to have the execute rights on the directory. Maybe you need to have write rights as well.
Therefore:
if [ -d "$DIRECTORY" ] && [ -x "$DIRECTORY" ] ; then
# ... to go to that directory (even if DIRECTORY is a link)
cd $DIRECTORY
pwd
fi
if [ -d "$DIRECTORY" ] && [ -w "$DIRECTORY" ] ; then
# ... to go to that directory and write something there (even if DIRECTORY is a link)
cd $DIRECTORY
touch foobar
fi

- 30,738
- 21
- 105
- 131
In kind of a ternary form,
[ -d "$directory" ] && echo "exist" || echo "not exist"
And with test
:
test -d "$directory" && echo "exist" || echo "not exist"

- 30,738
- 21
- 105
- 131

- 7,426
- 10
- 37
- 45
file="foo"
if [[ -e "$file" ]]; then echo "File Exists"; fi;

- 2,379
- 2
- 26
- 42
-
An explanation would be in order (by [editing your answer](https://stackoverflow.com/posts/15015952/edit), not here in comments). – Peter Mortensen May 07 '20 at 12:07
The ls
command in conjunction with -l
(long listing) option returns attributes information about files and directories.
In particular the first character of ls -l
output it is usually a d
or a -
(dash). In case of a d
the one listed is a directory for sure.
The following command in just one line will tell you if the given ISDIR
variable contains a path to a directory or not:
[[ $(ls -ld "$ISDIR" | cut -c1) == 'd' ]] &&
echo "YES, $ISDIR is a directory." ||
echo "Sorry, $ISDIR is not a directory"
Practical usage:
[claudio@nowhere ~]$ ISDIR="$HOME/Music"
[claudio@nowhere ~]$ ls -ld "$ISDIR"
drwxr-xr-x. 2 claudio claudio 4096 Aug 23 00:02 /home/claudio/Music
[claudio@nowhere ~]$ [[ $(ls -ld "$ISDIR" | cut -c1) == 'd' ]] &&
echo "YES, $ISDIR is a directory." ||
echo "Sorry, $ISDIR is not a directory"
YES, /home/claudio/Music is a directory.
[claudio@nowhere ~]$ touch "empty file.txt"
[claudio@nowhere ~]$ ISDIR="$HOME/empty file.txt"
[claudio@nowhere ~]$ [[ $(ls -ld "$ISDIR" | cut -c1) == 'd' ]] &&
echo "YES, $ISDIR is a directory." ||
echo "Sorry, $ISDIR is not a directoy"
Sorry, /home/claudio/empty file.txt is not a directory

- 8,088
- 1
- 43
- 57

- 6,939
- 2
- 22
- 20
-
+1, but it when ISDIR does not exist at all you get an error message as well as your diagnostics message. – ysap Feb 18 '13 at 17:46
The below find
can be used,
find . -type d -name dirname -prune -print

- 30,738
- 21
- 105
- 131

- 264
- 5
- 14
There are great solutions out there, but ultimately every script will fail if you're not in the right directory. So code like this:
if [ -d "$LINK_OR_DIR" ]; then
if [ -L "$LINK_OR_DIR" ]; then
# It is a symlink!
# Symbolic link specific commands go here
rm "$LINK_OR_DIR"
else
# It's a directory!
# Directory command goes here
rmdir "$LINK_OR_DIR"
fi
fi
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:
dir=" "
echo "Input directory name to search for:"
read dir
find $HOME -name $dir -type d
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 standard output (not an elegant solution for my taste) and will have nonetheless a zero exit. Maybe someone could improve on this.

- 30,738
- 21
- 105
- 131

- 103
- 1
- 3
-
13I'd be offended if a program went looking through my entire hard drive to find a directory rather than just politely looking in my current working directory or using the absolute path I give it. What you've suggested might be nice for a tool named `locate` but not nice for anything else... – sarnold Feb 01 '12 at 09:29
(1)
[ -d Piyush_Drv1 ] && echo ""Exists"" || echo "Not Exists"
(2)
[ `find . -type d -name Piyush_Drv1 -print | wc -l` -eq 1 ] && echo Exists || echo "Not Exists"
(3)
[[ -d run_dir && ! -L run_dir ]] && echo Exists || echo "Not Exists"
If an issue is found with one of the approaches provided above:
With the ls
command; the cases when a 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]

- 30,738
- 21
- 105
- 131

- 281
- 4
- 13
-
What does "above" refer to? The three command lines in this answer or previous answers? (Please respond by [editing your answer](https://stackoverflow.com/posts/15015952/edit), not here in comments. Thanks in advance.). – Peter Mortensen May 07 '20 at 12:18
Use the file
program.
Considering all directories are also files in Linux, issuing the following command would suffice:
file $directory_name
Checking a nonexistent file: file blah
Output: cannot open 'blah' (No such file or directory)
Checking an existing directory: file bluh
Output: bluh: directory

- 30,738
- 21
- 105
- 131

- 2,165
- 1
- 27
- 26
-
1Nice idea, however too bad in both cases the file command returns 0 exit code always. Also when the file/directory can not be found :(. – Melroy van den Berg Jun 07 '19 at 01:03
If you want to check if a directory exists, regardless if it's a real directory or a symlink, use this:
ls $DIR
if [ $? != 0 ]; then
echo "Directory $DIR already exists!"
exit 1;
fi
echo "Directory $DIR does not exist..."
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,587
- 2
- 20
- 31
-
3Alternatively you can use this shorter version: `if ! ls $DIR 2>/dev/null; then echo "$DIR does not exist!"; fi` – derFunk Jan 08 '14 at 10:51
-
```ls``` returning non-zero DOES NOT mean that the directory exists. The code should be: ```ls $DIR RETURN_CODE=$? if [ "$RETURN_CODE" != "0" ]; then echo "Cannot access directory $DIR (does not exist or no permission or something else altogether)!" exit $RETURN_CODE; fi echo "Directory $DIR already exists..."``` – zerzevul Feb 19 '21 at 13:25
From script file myScript.sh:
if [ -d /home/ec2-user/apache-tomcat-8.5.5/webapps/Gene\ Directory ]; then
echo "Directory exists!"
echo "Great"
fi
Or
if [ -d '/home/ec2-user/apache-tomcat-8.5.5/webapps/Gene Directory' ]; then
echo "Directory exists!"
echo "Great"
fi

- 30,738
- 21
- 105
- 131

- 10,819
- 1
- 66
- 58
Git Bash + Dropbox + Windows:
None of the other solutions worked for my Dropbox folder, which was weird because I can Git push to a Dropbox symbolic path.
#!/bin/bash
dbox="~/Dropbox/"
result=0
prv=$(pwd) && eval "cd $dbox" && result=1 && cd "$prv"
echo $result
read -p "Press Enter To Continue:"
You'll probably want to know how to successfully navigate to Dropbox from Bash as well. So here is the script in its entirety.

- 30,738
- 21
- 105
- 131

- 3,611
- 30
- 17
-
`cd -` can simplify this a lot... Or `cd "$OLDPWD"` (Or use a subshell that your script's dir doesn't change, but then you can't set variables) – Gert van den Berg Apr 18 '18 at 13:07
-
`prv=$(pwd) && eval "cd $dbox" && result=1 && cd "$prv"` is probably better-written as `if (cd "$dbox"); then result=1; fi` -- the `(...)` will "undo" the change of directory when it exits the parentheses. Also I would use `eval` only as a last resort, and with apologies in the comments. – amphetamachine Jul 08 '22 at 16:21
Just as an alternative to the '[ -d ]' and '[ -h ]' options, you can make use of stat
to obtain the file type and parse it.
#! /bin/bash
MY_DIR=$1
NODE_TYPE=$(stat -c '%F' ${MY_DIR} 2>/dev/null)
case "${NODE_TYPE}" in
"directory") echo $MY_DIR;;
"symbolic link") echo $(readlink $MY_DIR);;
"") echo "$MY_DIR does not exist";;
*) echo "$NODE_TYPE is unsupported";;
esac
exit 0
Test data:
$ mkdir tmp
$ ln -s tmp derp
$ touch a.txt
$ ./dir.sh tmp
tmp
$ ./dir.sh derp
tmp
$ ./dir.sh a.txt
regular file is unsupported
$ ./dir.sh god
god does not exist

- 30,738
- 21
- 105
- 131

- 3,293
- 1
- 9
- 22