Working with Bash script, we are in a need to check the existence of a directory. You can use the test command or its equivalent shorthand [ to check if a directory exists or not in a Bash shell script.
Here is an example:
#!/bin/bash
if [ -d "/path/to/directory" ]
then
echo "Directory exists."
else
echo "Directory does not exist."
fi
In this example, the -d option tests whether the directory /path/to/directory exists or not. If the directory exists, the script prints "Directory exists." If the directory does not exist, the script prints "Directory does not exist."
You can replace /path/to/directory with the path of the directory you want to check in your script.
Comments (0)