How would I go about creating a symlink that creates multiple links to one file?
I would like ~/Desktop/foo.txt
to "appear" in multiple locations; and the following doesn't work:
ln -s ~/Desktop/foo.txt /location/one /location/two
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.
Sign up to join this communityCheck the man page -- you can only specify one destination. You'll have to loop:
for destination in /location/one /location/two ...; do
ln -s ~/Desktop/foo "$destination"
done
For reference, in order to copy .gpl colour palettes into the appropriate GIMP, Inkscape and LibreOffice directories I did this:
read -e -p "Enter filename: " file
for destination in ~/.config/libreoffice/4/user/config/ ~/.config/inkscape/palettes/ ~/.gimp-2.8/palettes/ ...; do
ln --symbolic --backup --force "$file" "$destination"
done
Relatively simple python one-liner can do the job:
$ python -c 'import sys,os;map(lambda x:os.symlink(sys.argv[1],x),sys.argv[2:])' ~/input.txt ~/Desktop/input.txt ~/Pictures/input.txt
$ ls -l ~/Desktop/input.txt
lrwxrwxrwx 1 xieerqi xieerqi 23 2月 4 19:10 /home/xieerqi/Desktop/input.txt -> /home/xieerqi/input.txt
The way this works is simple. We use sys
module for processing command-line arguments, and use symlink()
function from os
module. The map()
function essentially is used as replacement for the for
loop, which takes a function and list as argument. Here, the function is lambda x:os.symlink(sys.argv[1],x)
, and it will be executed for each item in sys.argv[2:]
list.
Note, ~/input.txt
is original file (referred to as sys.argv[1]
, second command-line argument ), and ~/Desktop/input.txt
and ~/Pictures/input.txt
are resulting symlinks. They are arguments 2 and 3, thus we use sys.argv[2:]
list slice starting from 3rd item till the end of list. You may be wondering , well , where is the first argument. That's -c
flag.
A script form of the same thing would be
#!/usr/bin/env python
import sys,os
for item in sys.argv[2:]:
os.symlink(sys.argv[1],item)
You need two commands.
ln -s Desktop/foo.txt location1; ln -s Desktop/foo.txt location2
This example will create two links vi
and view
for the vim binary with verbose output.
$ for link in /usr/bin/{vi,view}; do ln -sv /usr/bin/vim $link; done;
'/usr/bin/vi' -> '/usr/bin/vim'
'/usr/bin/view' -> '/usr/bin/vim'