How to create a link to an existing file or directory using a GNU Linux shell command?
Asked
Active
Viewed 8.5e+01k times
453
-
@jcollum: Probably so... [UnixSE has this similar Q&A for example](https://unix.stackexchange.com/questions/84175/create-a-symbolic-link-relative-to-the-current-directory), but the selected answer here seems a better one. – Jul 18 '20 at 08:29
-
ln -s /data/data/com.termux/files/usr/lib/python3.9/site-packages /data/data/com.termux/files/usr/lib/python3.10/site-packages – CS QGB Nov 02 '21 at 10:59
2 Answers
808
Symbolic or soft link (files or directories, more flexible and self documenting)
# Source Link
ln -s /home/jake/doc/test/2000/something /home/jake/xxx
Hard link (files only, less flexible and not self documenting)
# Source Link
ln /home/jake/doc/test/2000/something /home/jake/xxx
More information: man ln
/home/jake/xxx
is like a new directory. To avoid "is not a directory: No such file or directory" error, as @trlkly comment, use relative path in the target, that is, using the example:
cd /home/jake/
ln -s /home/jake/doc/test/2000/something xxx

Cirelli94
- 1,714
- 1
- 15
- 24

theglauber
- 28,367
- 7
- 29
- 47
-
60Do note that you have to use a full path for this syntax. I wound up having to use `ln "$(pwd)/relative_path" xxx` in order to get an absolute link for `xxx` using a relative path. Apparently, bash clobbering rules are not expanded for the SOURCE. – trlkly Jan 06 '16 at 03:24
-
-
1
-
-
`ln` does not achieve the desired result. The resulting path is `/home/jake/xxx`, not `/home..../something`. – Peter L Apr 17 '18 at 17:51
-
1As trlkly said, write the full path manually. The "ln" command does not expand even the home directory "~". – Anton Tarasenko Nov 10 '19 at 15:31
-
The second file parameter is the link-name, the first one the `target` according to `man ln` I think, Your write that the second arg is the target. – Timo May 24 '21 at 18:38
-
The soft link to a directory worked if I just left out the trailing slash. The link path didn't need to be relative. – Lupilum Feb 10 '23 at 14:59
60
you should use :
ln -s /home/jake/doc/test/2000/something xxx

WiseTechi
- 3,528
- 1
- 22
- 15
-
as [trlkly](https://stackoverflow.com/users/1802924/trlkly) mentioned as a comment in this [answer](https://stackoverflow.com/a/9587490/6474744), I had to use the full path for both the source and the link. – Pedram Apr 25 '20 at 05:58