360

How to create a link xxx to /home/jake/doc/test/2000/something/?

Assume the xxx is created under /home/jake and you're currently in /home/jake. When you do cd xxx, you directly go to /home/jake/doc/test/2000/something/.

Gerold Broser
  • 11,355
  • 4
  • 36
  • 85
read Read
  • 4,797
  • 4
  • 24
  • 29
  • 10
    Why is this question `off-topic`? – Eyal Levin May 03 '20 at 10:37
  • 2
    it might be considered to be a question that belongs on unix.stackexchange.com – jcollum Jun 16 '20 at 22:21
  • @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. – Seamus Jul 18 '20 at 08:29

2 Answers2

669

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:

  1. cd /home/jake/
  2. ln -s /home/jake/doc/test/2000/something xxx
Cirelli94
  • 1,143
  • 13
  • 19
theglauber
  • 25,525
  • 7
  • 27
  • 45
  • 54
    Do 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
  • ln -s /home/jake/destination /home/jake/link_name – Turako Jul 16 '16 at 10:08
  • 1
    'hard link not allowed for directory' is what my debian says – zbig Feb 17 '17 at 17:12
  • @zbig As it says, hard link is files only. For dir, use `ln -s` – Azuaron Mar 08 '17 at 19:10
  • `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
  • 1
    As 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
52

you should use :

ln -s /home/jake/doc/test/2000/something xxx
WiseTechi
  • 3,348
  • 20
  • 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