12/22/2006
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
Leave a reply



Cool idea! I’ll try this out. Happy holidays!
Happy holidays to you too, Jud!
[...] 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. [...]
Both BSD and GNU tail support a -F flag that effectively does the same thing — watch for a non-existent file.