Ln

The ln command creates a link from one file to another.

Links are a feature of a filesystem so support for links depend on the filesystem where the link is created. The description that follows applies to the ext3 filesystem, but most common filesystems under Linux will behave similar.

Links are divided into two classes, hard and symbolic. Symbolic links (symlinks) are often preferred due to their flexibility and the ability to work across different partitions. Hard links are indistinguishable from the original and have their own restrictions.

Given a file, it is non-trivial to locate other files linked to it. Symbolic links can be found by searching the entire filesystem for symbolic links pointing to that file. Hard links, on the other hand, can only be found by searching every inode at a low level for inodes that share the same data pointers as that file, or by searching every directory for matching inode pointers.

= Hard = Hard links are implemented in UFS-style filesystems (ext2/3, etc) as different entries in directory blocks that point to the same inode. Renaming or deleting one of the entries does not remove the data, it only decrements a link count. Since the directory entries share the same inode, all metadata about the file is the same, excluding the name. The link names are kept in the directory entry and not in the inode. Hard links cannot cross partitions. In addition, directories cannot be hard linked by the user, only special links . and .. which refer to itself and the parent directory respectively exist. Also unlike symbolic links, hard links can never be broken by moving one of the files (directory entries) to another location.

With hard links, a link counter is maintained on the inode of each file. When the link counter drops to zero, the inode is deleted and the data blocks are freed for reuse. This way, once a file is hard linked, either "copy" of the file can be deleted, and the other will be unchanged. Normally, files only have a single link. Directories have as many links as they have subdirectories (including . and ..).

Creating
Hard links are created with ln source link Where source is an existing file, and link is the file to create. link is optional. If it is not given, the filename given in source is used. If link is an existing directory, it will create the new link there.

Finding
Locating a hard link is a bit harder than a symbolic link. In ls -l, the number following the permissions but before the owner is the number of links to that file. For normal files, if that number is greater than 1, then that file is hardlinked somewhere else as well as the currently listed one: lrwxrwxrwx  2  username groupname  # 2004-03-08 15:55  foo

Hard linked files can also be found with find -type f -links +1

This will give all files with more than one link to them.

You can also identify if two files share an inode by running stat file on each file. You will note an 'Inode field which gives the unique inode number.

Modifying
It should be noted that modifying a file with multiple hard-links usually means that the other links are also affected. For example:

$ echo "Contents of file 1." > file1 $ ln file1 file2 $ cat file2 Contents of file 1. $ echo "Contents of file 2." > file2 $ cat file1 Contents of file 2.

An exception to this general rule is emacs. With emacs, the original, crosslinked file is saved with a tilde (~). $ emacs file1      # Change the contents, save, and exit $ ls -l -rw-rw-r--   1 Snags    Snags          19 Aug 17 14:56 file1 -rw-rw-r--   2 Snags    Snags          19 Aug 17 14:56 file1~ -rw-rw-r--   2 Snags    Snags          19 Aug 17 14:56 file2 The number just before the username is the number of hard links.

Because of this, care should be exercised in hard-linking files that are likely to be modified.

= Symbolic = Symbolic links, (often referred to simply as "symlinks"), are implemented in Linux as a separate file containing a reference in the file to the original file, in more technical terms, an inode containing the "real" location of the file. Symbolic links use a path that can be either absolute (beginning with /) or relative to the location of the link. If a relative link is made and then moved, it will be broken, whereas absolute links generally will not.

Because of their design, a symbolic link can point to any directory or file, or even point to a non-existent location (in which case it is called "broken" or, rarely, "dangling"). A program called symlinks was written to make the process of locating and cleaning up broken symlinks. Unlike a hard link, deleting the original file will cause the file to be erased and the symlinks pointing to it to become dangling.

Symbolic links have no permissions or status of their own. Instead, they use the permissions of the file they point to though the permissions of the link itself are lrwxrwxrwx. The size of a symlink is reported as the length of the path they contain (plus the null character at the end)

Creating
Symbolic Links are created with the -s</tt> flag to ln</tt>: ln -s source link Where source is the "text" of the symlink, and link is the actual file which will be created. source can be any string, even one that does not point to a file (in which case, the symlink will link nowhere), and it will be used exactly as given. This means that ln -s sbin/executable bin/executable</tt> will not create a link from bin/executable to sbin/executable, instead, bin/executable will point to bin/sbin/executable.

The link is optional. If a name is not given here, it will assume the link should be named the same as the file in content. If an existing directory name is provided with no filename, it will create the link in that directory.

Example: ln -s /docs/saved/emails/customheader.odt -- Creates a link in the current directory named 'customheader.odt'

Finding
Using ls -l</tt> to locate symbolic links is easy. They are shown as lrwxrwxrwx  1  username groupname  # 2004-03-08 15:55  foo -> bar This indicates that anytime foo</tt> is accessed, what is ultimately accessed is the file named bar</tt>.

Symbolic links can also be located using find -type l

= Provided by =

Most (all?) Linux distributions incorporate this from the GNU Coreutils: man page

= Related Commands =
 * link - Make hard links.
 * mkdir - Make directories.
 * mkfifo - Make a named pipe directory entry (aka fifo)
 * mknod - Make block or (possibly unbuffered) character special file, or a socket.
 * readlink - Make a canonical name.
 * rmdir - Remove empty directories.
 * unlink - Remove directory entries (other than for subdirectories)

= See Also =
 * file
 * device
 * Directory
 * link(function)
 * symlink(function)