Check if a directory exists
This page was last updated on January 19, 2017.
- Use this Bash script, replacing /test with the full path to a possible directory, to check if the directory exists and tell you the result:
-
#!/bin/bash target="/test" echo "Checking for the $target directory"; if [ -d $target ]; then echo "The $target directory exists."; else echo "The $target directory does not exist."; fi;
- Use this Bash script, replacing /test with the full path to a possible directory, to check if the directory exists, create it if it doesn’t, and tell you the results:
-
#!/bin/bash target="/test" echo "Checking for the $target directory"; if [ -d $target ]; then echo "The $target directory exists."; else mkdir $target; echo "The $target directory did not exist and has been created."; fi;
Obligatory Happy Ending
And they all lived happily ever after. The end.

Comment: