Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 37457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:33:13+00:00 2026-05-10T14:33:13+00:00

What command checks if a directory exists or not within a Bash shell script?

  • 0

What command checks if a directory exists or not within a Bash shell script?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T14:33:14+00:00Added an answer on May 10, 2026 at 2:33 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that checks if a file exists or not using the
How safe are command lists in shell (or bash) from race conditions? if [
Hey, I started bash shell scripting and I'm trying to make a script for
In ksh shell, I wanna to check the return value after running a command,
I have as command-line parameters to my batch script a list of filenames and
Shell scripting newbie here. Just wanted some input on my shell script. I basically
I've created a PHP Daemon script to continually monitor a particular directory on the
Since I write a command line program to check cpp files, and it need
Is there anyway to check if a window.history.go command is successful in changing the
Are there equivalent to perl -c syntax check for JavaScript from command? Given that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.