cmd/ln: Difference between revisions

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
clarification & tidying
No edit summary
 
Line 1: Line 1:
[[category:Linux]]
[[category:Linux]]
'''<code>ln</code>''' is the Linux command for creating a link to a file or folder.
{{fmt/title|command: <code>ln</code>}}
==About==
'''<code>ln</code>''' is the [[Linux]] command for creating a link to a file or folder.


This page so far only discusses ''symbolic'' links, not ''hard'' links. (TODO)
This page so far only discusses ''symbolic'' links, not ''hard'' links. ({{hashtag|TODO}})
==General Syntax==
==General Syntax==
  ln {{fmt/opt|-r}} --symbolic {{arg|filespec of existing file/folder}} {{arg|filespec for new link}}
  ln {{fmt/opt|-r}} --symbolic {{arg|filespec of existing file/folder}} {{arg|filespec for new link}}

Latest revision as of 13:32, 14 December 2025

Template:Fmt/title

About

ln is the Linux command for creating a link to a file or folder.

This page so far only discusses symbolic links, not hard links. (Template:Hashtag)

General Syntax

ln Template:Fmt/opt --symbolic Template:Arg Template:Arg

scenarios

  • If you're in the folder where you want the link to be created:
ln Template:Fmt/opt --symbolic Template:Arg ./Template:Arg
  • If you're in the folder where the existing/real file is:
ln Template:Fmt/opt --symbolic ./Template:Arg Template:Arg

notes

  • A link cannot have the same name as a folder in the same directory (same rule as if it were a regular file).
  • The -r option just tells ln to calculate the relative path from the link to the existing file, and use that as the link URL; see "Relative Links" below.

Errors

  • The error message "ln: failed to create symbolic link 'Template:Arg': File exists" can be maddeningly misleading. It seems to always report the existing file, even when the problem is actually that the second filename (the link-name) already exists. Translation:
    • "'Template:Arg': File exists" (arg 1 should be Template:Arg): The link-name is the same as an existing file, possibly a folder.
      • The fact that ln reports the first param instead of the second one seems to be a Template:L/sub. Yes, the first param is a file that does exist, but that's not the problem.
    • "'Template:Arg': File exists" (arg 2 should be Template:Arg): You've got the arguments backwards, and are trying to create a link under the same name as the existing file (to a file which doesn't exist).

Note that when ln creates a relative link using the -r option, all it is doing is calculating the relative path for you and then using that as the link's target-string (URL). You can create a relative link without -r by just specifying a relative path. There's no difference between an absolute link and a relative link except whether the URL uses any relative-path syntax (e.g. "../", or no initial "/").

Relative links will still work even if the folder containing both files (the original and the link) is moved or copied, which can be useful for making links within a portable code repository. (Git does store link-files by default.)

Notes