EN
Bash - check if directory exists
0 points
In this article, we would like to show you how to check if directory exists in Bash.
Quick solution:
xxxxxxxxxx
1
2
3
if [ -d "path/to/directory" ]
4
then
5
echo "directory exists."
6
fi
Hint:
If you want to check if directory doesn't exist, 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.
xxxxxxxxxx
1
2
3
DIRECTORY="$1"
4
5
if [ -d "$DIRECTORY" ]; then
6
echo "$DIRECTORY exists."
7
else
8
echo "$DIRECTORY does not exist."
9
fi
Run in command line:
xxxxxxxxxx
1
./script.sh test_directory
Note:
[ -d "path/to/directory" ]
is equivalent totest -d "/path/to/directory"
.