Browse - Programming Tips - What's the best way to ensure a directory exists in a bash script?
Q. What's the best way to ensure a directory exists in a bash script?
A. Like this:
if [[ ! -d $mydir ]]; then
mkdir -p $mydir || exit 1
We make the folder if it doesn't exist.
When making it we use the -p option to mkdir which will make its parents if necessary.
The idiom || exit means we'll exist the script if we can't make the folder.