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 Answers3

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
Serge
  • 8,541
  • 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 – Serge Jun 02 '16 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
don_crissti
  • 82,805
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.)

Stephen Kitt
  • 434,908