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

❤️

1 person reacted.

Hi, I'm who's behind Fedify, Hollo, BotKit, and this website, Hackers' Pub! My main account is at @hongminhee洪 民憙 (Hong Minhee).

Fedify, Hollo, BotKit, 그리고 보고 계신 이 사이트 Hackers' Pub을 만들고 있습니다. 제 메인 계정은: @hongminhee洪 民憙 (Hong Minhee).

FedifyHolloBotKit、そしてこのサイト、Hackers' Pubを作っています。私のメインアカウントは「@hongminhee洪 民憙 (Hong Minhee)」に。