EN
Bash - check if directory doesn't exist
0 points
In this article, we would like to show you how to check if directory exists using Bash.
Quick solution:
xxxxxxxxxx
1
2
3
if [ ! -d "path/to/directory" ]
4
then
5
echo "directory does not exist."
6
fi
Hint:
If you want to check if directory exists, go to this article.
In this example, we create a script into which we pass a path to directory (test_directory
in Usage example) as an argument from bash console.
script.sh
file:
xxxxxxxxxx
1
2
3
DIRECTORY="$1"
4
5
if [ ! -d "$DIRECTORY" ]; then
6
echo "$DIRECTORY does not exist."
7
else
8
echo "$DIRECTORY exists."
9
fi
Run in command line:
xxxxxxxxxx
1
./script.sh test_directory
Note:
[ ! -d "path/to/directory" ]
is equivalent totest ! -d "/path/to/directory"
.