Touch Create Directory and File?

Does anybody know if there is a way for touch (or any other command) to create a file and if the directory does not exists it will create it?

Thanks.

15" Unibody MacBook Pro (2.8 GHz, 4 GB, 320 7K GB, SD, AP, BT, APP), Mac OS X (10.6.2), 10.5 & 10.6 ACSP ACTC

Posted on Apr 5, 2010 7:51 PM

Reply
2 replies

Apr 6, 2010 3:37 PM in response to andyboutte

Here's a crude script. You could probably add a check to test
whether you have permission to write in the directory where you
are wanting to create the file.


!/bin/bash
BASENAME=/usr/bin/basename
DIRNAME=/usr/bin/dirname
MKDIR=/bin/mkdir
TOUCH=/usr/bin/touch
DIRECTORY=`${DIRNAME} ${1}`
FILENAME=`${BASENAME} ${1}`
if [[ ! -d ${DIRECTORY} ]]
then
${MKDIR} ${DIRECTORY}
fi
if [[ ! -f ${FILENAME} ]]
then
${TOUCH} ${DIRECTORY}/${FILENAME}
fi

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Touch Create Directory and File?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.