5

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
tarabyte
  • 2,585

1 Answers1

16
mkdir -p <path-to-desired-directory>

From the man page:

   -p, --parents
      no error if existing, make parent directories as needed
Itai
  • 3,387