468

How to create a link to an existing file or directory using a GNU Linux shell command?

2
  • @jcollum: Probably so... UnixSE has this similar Q&A for example, but the selected answer here seems a better one.
    – user5395338
    Jul 18, 2020 at 8: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 2, 2021 at 10:59

2 Answers 2

831

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
8
  • 66
    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 6, 2016 at 3:24
  • ln -s /home/jake/destination /home/jake/link_name
    – Turako
    Jul 16, 2016 at 10:08
  • 1
    'hard link not allowed for directory' is what my debian says
    – zbig
    Feb 17, 2017 at 17:12
  • 1
    @zbig As it says, hard link is files only. For dir, use ln -s
    – Azuaron
    Mar 8, 2017 at 19:10
  • 2
    As trlkly said, write the full path manually. The "ln" command does not expand even the home directory "~". Nov 10, 2019 at 15:31
63

you should use :

ln -s /home/jake/doc/test/2000/something xxx
1
  • as trlkly mentioned as a comment in this answer, I had to use the full path for both the source and the link.
    – Pedram
    Apr 25, 2020 at 5:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.