I love "mkdir -p", it is one of my favorite linux commands but, one thing that I wish, was there was a "-p" equivalent for "touch"

Follow

@mcc This inspired me to write and test a somewhat robust shellscript function for inclusion in ~/.shrc or ~/.bashrc:

touchp() { for file in "$@"; do mkdir -p "$( dirname "$file" )" && touch "$file"; done; }

@arielmt @mcc Nitpicking, but dirname is non posix. ${file%/*} accomplishes the same though.

@nico @mcc I relied on pubs.opengroup.org/onlinepubs/ for availability.

That noted, "${file%/*}" is both faster and more elegant. I like it. (It does fail silently and probably bad if a pathname ends with a slash, though.)

@arielmt @mcc OMG. I have to retract my previous statement and call myself old. It wasn't there last time I checked, I swear!

@arielmt @mcc as a point of note, i think this makes much more sense to be written as

touchp() { for file; do mkdir -p "$(dirname "$file")" && >> "$file"; done; }

(a) in "$@" is extraneous
(b) so is touch. unless of course you are actually using touch for touching instead of just making files, but seeing as it's most likely a fresh directory, that's unlikely

Sign in to participate in the conversation
Computer Fairies

Computer Fairies is a Mastodon instance that aims to be as queer, friendly and furry as possible. We welcome all kinds of computer fairies!