8

Pseudocode

ln -s $HOME/file $HOME/Documents/ $HOME/Desktop/ 

where I want to create a symlink from the source to two destinations. Probably, moreutils and pee.


How can you create many symlinks from one source?

3 Answers 3

11

You can't do this with a single invocation of ln,but you could loop through all necessary destinations:

$ for i in "$HOME/Documents/" "$HOME/Desktop/"; do ln -s "$HOME/file" "$i"; done 
1
  • 2
    You could omit the quotes if your home base directory does not contain spaces or other characters to be escaped, e.g. punctuation. However, using quotes always where appropriate makes a habit that keeps you from mistakes in other cases where directory/file names could easily contain special chars Commented Jun 2, 2016 at 15:04
6

If you have gnu parallel you could try with

parallel ln -s /path/file {} ::: /path/dest1 /path/dest2 /path/dest3 

or, to symlink multiple targets to (the same) multiple destinations

parallel ln -s {1} {2} ::: /path/file1 /path/file2 ::: /path/dest1 /path/dest2 
1
  • 2
    @Masi Make sure you run the parallel command from the parallel package and not the one from moreutils. Commented Jun 1, 2016 at 22:27
5

It's no less verbose than two separate ln -s invocations:

echo $HOME/Documents/ $HOME/Desktop/ | xargs -n 1 ln -s $HOME/file 

but that only works for absolute paths (because symbolic links are interpreted relative to their parent directory, unless they're absolute).

(The relative cost drops of course as the number of links goes up. Also, this snippet relies on the fact that $HOME doesn't contain any spaces, tabs or newlines.)

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.