Copyright © 2008-2018, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.
Date: 2016may4 OS: Linux Language: bash Q. What's the best way to ensure a directory exists in a bash script? A. Like this:mydir=/usr/local/one/two/three if [[ ! -d $mydir ]]; then mkdir -p $mydir || exit 1 fiWe 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.
Add a commentSign in to add a comment