Difference between revisions of "cmd/ln"
(clarification & tidying) |
|||
Line 2: | Line 2: | ||
'''<code>ln</code>''' is the Linux command for creating a link to a file or folder. | '''<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) | |
− | ln -r --symbolic {{arg|existing file}} {{arg|link | + | ==General Syntax== |
− | * | + | ln {{fmt/opt|-r}} --symbolic {{arg|filespec of existing file/folder}} {{arg|filespec for new link}} |
− | * | + | ===scenarios=== |
+ | * If you're in the folder '''where you want the link to be created''': | ||
+ | ln {{fmt/opt|-r}} --symbolic {{arg|path to existing file/folder}} ./{{arg|name for new link}} | ||
+ | * If you're in the folder '''where the existing/real file is''': | ||
+ | ln {{fmt/opt|-r}} --symbolic ./{{arg|name of existing file}} {{arg|path to new link}} | ||
+ | ===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 <code>-r</code> option just tells <code>ln</code> 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 '{{arg|filename}}': 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: | * The error message "ln: failed to create symbolic link '{{arg|filename}}': 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: | ||
** "<tt>'{{arg|arg 1}}': File exists</tt>" (arg 1 should be {{arg|existing file}}): The link-name is the same as an existing file, possibly a folder. | ** "<tt>'{{arg|arg 1}}': File exists</tt>" (arg 1 should be {{arg|existing file}}): The link-name is the same as an existing file, possibly a folder. | ||
*** The fact that <code>ln</code> reports the first param instead of the second one seems to be a {{l/sub|bug}}. Yes, the first param is a file that does exist, but that's not the problem. | *** The fact that <code>ln</code> reports the first param instead of the second one seems to be a {{l/sub|bug}}. Yes, the first param is a file that does exist, but that's not the problem. | ||
** "<tt>'{{arg|arg 2}}': File exists</tt>" (arg 2 should be {{arg|name for link}}): 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). | ** "<tt>'{{arg|arg 2}}': File exists</tt>" (arg 2 should be {{arg|name for link}}): 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). | ||
− | == | + | ==Relative Links== |
− | + | Note that when <code>ln</code> creates a relative link using the <code>-r</code> 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 <code>-r</code> 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== | ==Notes== | ||
* [http://stackoverflow.com/questions/1347105/linux-link-all-files-from-one-to-another-directory LINUX: Link all files from one to another directory] | * [http://stackoverflow.com/questions/1347105/linux-link-all-files-from-one-to-another-directory LINUX: Link all files from one to another directory] |
Latest revision as of 22:19, 11 December 2024
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. (TODO)
General Syntax
ln [-r] --symbolic <filespec of existing file/folder> <filespec for new link>
scenarios
- If you're in the folder where you want the link to be created:
ln [-r] --symbolic <path to existing file/folder> ./<name for new link>
- If you're in the folder where the existing/real file is:
ln [-r] --symbolic ./<name of existing file> <path to new link>
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 tellsln
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 '<filename>': 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:
- "'<arg 1>': File exists" (arg 1 should be <existing file>): 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 bug. Yes, the first param is a file that does exist, but that's not the problem.
- The fact that
- "'<arg 2>': File exists" (arg 2 should be <name for link>): 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).
- "'<arg 1>': File exists" (arg 1 should be <existing file>): The link-name is the same as an existing file, possibly a folder.
Relative Links
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.)