In
For checking directory existence we use
#bash #linux #directory_existence #file_existence
Linux
we have a command called test
, you can check whether a file/directory exists or not and run commands based on the result. For example let's say we want to check if a folder exists and if it does not exist, create the folder.For checking directory existence we use
test -d
and for file existence we use test -f
, so for our example in order to check if the directory exists we use test -d
and in case the folder does not exists we will create it:directory_to_check="/data/mysql"
test -d $directory_to_check || {
echo "$directory_to_check does not exist, creating the folder..." && mkdir -p $directory_to_check || {
echo "$directory_to_check directory could not be created!"
exit 1
}
}
NOTE:
you can read more about exit codes with hashtag #exit_code#bash #linux #directory_existence #file_existence