Bash – Use `ln` to create a missing directory

bashlnpackage-managementsymlink

So I'm writing a small package manager, and a problem I've run into is making the symbolic links to files.

It installs the package to /usr/pkg/name-version, and then reads a file to determine what symbolic links to make. I'm using ln to make the links, and I've run into a problem when trying to install the Linux API headers.
I need to link the header files themselves, not the folders that contain them (so if 2 packages need to put files in the same subdirectory of include they can without screwing one package up).

That problem I solved, but ln simply errors out if the path is incomplete, which is annoying because those directories shouldn't exist until the package is installed.

Is there a flag for ln that will create any directories that are missing, or am I going to have to go with some convoluted bash script?

Best Answer

You won't need a convoluted bash script, but a simple one-liner. mkdir --parents will take care of everything, nicely not even printing an error if the directory structure already exists.

Just be careful with how you treat these directories on removal, so you don't break other packages.

Also, since you're writting it in bash, you can take a look at sorcery (shameless plug). Maybe it would be simpler to just modify that, as it is mature and flexible.

Related Question