skip to Main Content

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

In a Bash shell script, you can check if a directory exists or not using the -d flag with the test command or its equivalent [ ] brackets. Here’s an example:



if [ -d "$directory" ]; then
    echo "Directory exists"
    echo "Directory does not exist"

In this example, the variable directory holds the path to the directory you want to check. The -d flag is used inside the [ ] brackets to test if the given path is a directory. If the directory exists, the condition evaluates to true, and the “Directory exists” message is printed. Otherwise, the “Directory does not exist” message is printed.

Make sure to replace "/path/to/directory" with the actual path you want to check. Additionally, you can modify the script to perform other actions based on the existence or non-existence of the directory.

A Self Motivated Web Developer Who Loves To Play With Codes...

This Post Has 0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top