TIL: Use install to create the directory layout on GNU/Linux.

Suppose you need to create a file at a nested path like /etc/systemd/zram-generator.conf.d/10-writeback.conf. (Happens all the time.)

That means you need to create the parent directories recursively as needed. The portable, POSIX way would be:

mkdir -p "$(dirname /etc/some/deep/path/to/your/file)" && : > /etc/some/deep/path/to/your/file

Eeek!

But install can come handy. Originally it's about copying the files from one directory to another. So install SOURCE DEST copies files in SOURCE to DEST. But there's -D option:

-D: create all leading components of DEST except the last, or all components of --target-directory, then copy SOURCE to DEST

Together with a good default mod (-m 0644), you can:

install -m 0644 -D /dev/null /etc/some/deep/path/to/your/file

...And it works! Far more memorable, plus it works nicely with Fish's powerful history autocompletion. No need to add a custom function or script.

The -D option is GNU/Linux only. On other systems like macOS, you will have to:

install -d "$(dirname /path/to/file)" && install -m 0644 /dev/null /path/to/file

But this would be worse than the mkdir -p circus above.

1

If you have a fediverse account, you can reply to this note from your own instance. Search https://hackers.pub/ap/notes/0199cc97-7860-75ee-8012-8093cf016d73 on your instance and reply to it.