Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a symbolic link when target directory doesn't exist?

Is there a flag you can add to the ln command to force the creation of the target directory structure (like mkdir -p).

Consider :

ln -s /Applications/'Sublime Text.app'/Contents/SharedSupport/bin/subl /usr/local/bin/

Which adds a symlink to the Sublime Text command line tool. But it fails if /usr/local/bin/ doesn't exist.

I've tried the -f 'force' flag but that doesn't help.

Do you need to test wether /usr/local/bin/ exists and create it if it doesn't before running the ln command or is there a fancier way to do it?

like image 241
dwkns Avatar asked Oct 15 '14 09:10

dwkns


2 Answers

Try like this,

mkdir -p  /create_your_path/ && xargs ln -s /link_file_path/file /create_your_path/
like image 73
Adem Öztaş Avatar answered Oct 06 '22 23:10

Adem Öztaş


Nope, there is no standard option to ln to create a missing target directory. Use install or mkdir -p before ln, perhaps in a helper script if you find you need this more than once.

#!/bin/bash -e
mkdir -p "${!#}"
exec ln -s "$@"

This is obviously not very robust, but feel free to embellish it with more sanity checks if you think you really find it useful.

like image 22
tripleee Avatar answered Oct 06 '22 23:10

tripleee