Rocking the blogosphere
Apple Online Store

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

Del.icio.us Digg Reddit Technorati

Possibly related posts

Comments

  1. Jud
    December 23rd, 2006 | 12:43 pm

    Cool idea! I’ll try this out. Happy holidays!

  2. December 23rd, 2006 | 12:58 pm

    Happy holidays to you too, Jud!

  3. December 28th, 2006 | 3:16 pm

    [...] The other day I realized that often when I’m using touch, I’d like it to have the ability to create ancestor directories that don’t exist, a la mkdir -p. I quickly hacked together a shell script to do what I want. [...]

  4. Anonymous
    July 9th, 2008 | 4:52 pm

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

Leave a reply

Apple Online Store
Apple Online Store