Last Updated: February 25, 2016
· mroth

canhaz: recursive touch in shell

If you're like me, you use touch to create new files often. Often, you end up trying to create a file in a directory that doesn't yet exist, so you end up getting an error message, creating the directory with mkdir -p, and then running the touch command a second time. Well, that's a waste of time!

Drop the following shell function in your .bashrc or .zshrc or whatever wacky thing you use, and have a handy shortcut for the future.

# recursively touch, e.g. touch + mkdir -p
# so files can easily be created at depth
canhaz () {
  mkdir -p -- "${1%/*}" && touch -- "$1"
Say Thanks
Filed Under