4

This seems like a general utility and am wondering if anything already exists. I'm trying to place a timestamped logfile in a directory after a script runs, but need to make sure the subdirectory (and any directories along the chain) exist first.

For example, if the full relative path is ./2014/Backups/Logfiles, I'd like to check if 2014 does not exist, create it, if Backups does not exist, create it, if Logfiles does not exist, create it....

So far I've got something like this that (but it's only one step): LOGFILE_DIRECTORY="./2014/Backups/Logfiles"

if [ ! -d "$LOGFILE_DIRECTORY" ]; then
  mkdir "$LOGFILE_DIRECTORY" >> $LOGFILE 2>&1
fi

EDIT, Adding solution

if [ ! -d "$LOGFILE_DIRECTORY" ]; then
  mkdir -p "$LOGFILE_DIRECTORY" >> $LOGFILE 2>&1
fi
12
mkdir -p <path-to-desired-directory>

From the man page:

   -p, --parents
      no error if existing, make parent directories as needed
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.