How do I check if a directory exists in a Bash shell script

0 votes
What command checks if a directory exists or not, within a Bash shell script?
Jun 7 in Linux Administration by Rahul
• 3,380 points
17 views

1 answer to this question.

0 votes

To check if a directory exists in a shell script, you can use the following:

if [ -d "$DIRECTORY" ]; then
  # Control will enter here if $DIRECTORY exists.
fi

Or to check if a directory doesn't exist:

if [ ! -d "$DIRECTORY" ]; then
  # Control will enter here if $DIRECTORY doesn't 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. If the variables contain spaces or other unusual characters it will probably cause the script to fail.

answered Jun 7 by Korak
• 5,820 points

Related Questions In Linux Administration

0 votes
2 answers

How do I use a shell script to SSH in to a remote machine to execute commands?

Sorry in advance for any formatting. Check out ...READ MORE

answered Feb 7, 2021 in Linux Administration by anonymous
13,799 views
0 votes
0 answers

How can I invoke both BASH and CSH shells in a same script

In the same script, I want to ...READ MORE

Jun 21 in Linux Administration by Korak
• 5,820 points
8 views
0 votes
0 answers

How do I run a program with commandline arguments using GDB within a Bash script?

When running a program on GDB, usually, ...READ MORE

Jun 21 in Linux Administration by Korak
• 5,820 points
3 views
0 votes
0 answers
0 votes
1 answer

How to find the first field from a file in Bash Shell?

Hi@akhtar, You can extract text from a file. ...READ MORE

answered Oct 20, 2020 in Linux Administration by MD
• 95,360 points
280 views
0 votes
0 answers

How do I get my bash script to assign unique usernames automatically to new users?

I'm a beginner writing a bash script ...READ MORE

6 days ago in Linux Administration by Korak
• 5,820 points
12 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to check if a string contains a substring in Bash?

You ought to recollect that shell prearranging ...READ MORE

answered May 27 in Linux Administration by Korak
• 5,820 points
17 views
0 votes
1 answer

Is there a limit on the number of words in bash command 'for file in words'?

Anyway, you need to print everything except ...READ MORE

answered May 27 in Linux Administration by Korak
• 5,820 points
16 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP