How to create multiple soft links to one file?
Solution 1
Check 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
Solution 2
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)
Related videos on Youtube
uc50ic4more
Updated on September 18, 2022Comments
-
uc50ic4more about 1 year
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