Adding a -p option to touch

I’ve sometimes wished that the UNIX touch command had the same -p option as mkdir. With a little bit of scripting, it can:

#!/bin/sh

mkdir="/bin/mkdir"
touch="/usr/bin/touch"

for arg in $*; do
    if [ "$arg" = "-p" ]; then
        opt_p=true
        continue
    fi
    if [ "$opt_p" = "true" ]; then
        $mkdir -p $(dirname $arg) && $touch $arg
    else
        $touch $arg
    fi
done

4 thoughts on “Adding a -p option to touch

  1. Pingback: Marc Abramowitz » Patch for GNU touch to add -p option, a la mkdir

  2. Both BSD and GNU tail support a -F flag that effectively does the same thing — watch for a non-existent file.

Leave a Reply

Your email address will not be published.