Ln (Unix)
Encyclopedia
ln is a standard Unix
Unix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...

 command
Command (computing)
In computing, a command is a directive to a computer program acting as an interpreter of some kind, in order to perform a specific task. Most commonly a command is a directive to some kind of command line interface, such as a shell....

 used to create links (link) to files.

Link files

Links allow more than one file name to refer to the same file, elsewhere.

There are two types of links, both of which are created by ln:
  • symbolic link
    Symbolic link
    In computing, a symbolic link is a special type of file that contains a reference to another file or directory in the form of an absolute or relative path and that affects pathname resolution. Symbolic links were already present by 1978 in mini-computer operating systems from DEC and Data...

    s, which refer to a symbolic path indicating the abstract location of another file, and
  • hard link
    Hard link
    In computing, a hard link is a directory entry that associates a name with a file on a file system. . The term is used in file systems which allow multiple hard links to be created for the same file. This has the effect of creating multiple names for the same file, causing an aliasing effect: e.g...

    s, which refer to the specific location of physical data.


These links behave differently when the source of the link (what is being linked to) is moved or removed. Symbolic links are not updated (they merely contain a string which is the pathname of its target); hard links always refer to the source, even if moved or removed.

Specification

The Single Unix Specification
Single UNIX Specification
The Single UNIX Specification is the collective name of a family of standards for computer operating systems to qualify for the name "Unix"...

 (SUS) specifies the behaviour that a link or links (either symbolic or hard) will be created where specified that will link to the target file (or directory) specified.

More precisely, ln can be invoked in one of two ways: two arguments—first an argument specifying the source file then the target, or multiple (greater than two) arguments, specifying firstly a number of source files, then a directory in which all the links are to be created. In the latter invocation, the names of the links will be that of the source file. This invocation will be assumed if the last argument is a directory. If invoked in the latter form, ln's behaviour is not specified (it is implementation-defined).

ln is specified to use behaviour identical to that of the standard unlink and link functions.
However, we still have access to the original data through the hardlink:

~/directory $ cat hardlink.file
This is a file

You will notice that when we deleted the original file, the hardlink didn't vanish. Similarly, if we had deleted the softlink, the original file wouldn't have vanished.

Usage

The SUS mandates for ln two options: -f will force removal of existing files to allow the link to be created; and -s will create symbolic links. Therefore, ln with no options creates a hard link, ln -f forces a hard link, ln -s creates a symbolic link, and ln -fs forces a symbolic link. In order to link to a folder (vs. a file), use the -n option so that the symbolic link is not dereferenced: ln -sfn source/folder linked/folder/name.

Other Unix and Unix-like
Unix-like
A Unix-like operating system is one that behaves in a manner similar to a Unix system, while not necessarily conforming to or being certified to any version of the Single UNIX Specification....

 operating systems may add extra options. GNU ln adds options such as -b to back up files before creating links, -v to print out the name of each file before links are created, and others. BSD ln adds -h, preventing ln from descending into targets whose symlinks point to directories


ln file_name link_name


would have the effect of creating a hard link called link_name that points to the same data as the existing file file_name.

Symbolic link creation and deletion

The following shows the creation of a symbolic link slink.txt:

$ ln -s data.txt slink.txt
$ ls -li
969768 -rw-r--r-- 1 alex alex 10 Dec 9 09:11 data.txt
969817 lrwxrwxrwx 1 alex alex 8 Dec 9 09:11 slink.txt -> data.txt


The symbolic (soft) link is stored in a different inode than the text file (969817). The information stored in data.txt is accessible through the slink.txt:

$ file slink.txt
slink.txt: symbolic link to `data.txt'
$ cat slink.txt
some data

If we delete the text file data.txt, slink.txt becomes a broken link
Broken link
Broken link may refer to:* "Broken Link" , the final fourth-season episode of Star Trek: Deep Space Nine* Broken or dead link, a link that, having suffered link rot, points to a webpage or server that is no longer available on the World Wide Web* Broken link , a symbolic link that points to a file...

 and our data is lost.

$ rm data.txt
$ ls -li
969817 lrwxrwxrwx 1 alex alex 8 Dec 9 09:11 slink.txt -> data.txt
$ file slink.txt
slink.txt: broken symbolic link to `data.txt'
$ cat slink.txt
cat: slink.txt: No such file or directory

Hard link

If hlink.txt was a hard link
Hard link
In computing, a hard link is a directory entry that associates a name with a file on a file system. . The term is used in file systems which allow multiple hard links to be created for the same file. This has the effect of creating multiple names for the same file, causing an aliasing effect: e.g...

, our data would still be accessible through hlink.txt.
Also, if you delete the original file, the hard-linked copy would still be there:


$ ln data.txt hlink.txt

$ ls -li

104690 -rw-r--r-- 2 sc69876 support 10 Aug 29 18:13 data.txt
104690 -rw-r--r-- 2 sc69876 support 10 Aug 29 18:13 hlink.txt
$ rm data.txt

$ ls -li

104690 -rw-r--r-- 1 sc69876 support 10 Aug 29 18:13 hlink.txt
$ cat hlink.txt
some data



Difference b/w Hard Link and Symbolic (soft) link

Unix files consist of two parts: the data part and the filename part.

The data part is associated with something called an 'inode'. The inode carries the map of where the data is, the file permissions, etc. for the data.
.---------------> ! data ! ! data ! etc
/ +------+ !------+
! permbits, etc ! data addresses !
+------------inode---------------+
The filename part carries a name and an associated inode number.

.--------------> ! permbits, etc ! addresses !
/ +---------inode-------------+
! filename ! inode # !
+--------------------+

More than one filename can reference the same inode number; these files are said to be 'hard linked' together.

! filename ! inode # !
+--------------------+
\
>--------------> ! permbits, etc ! addresses !
/ +---------inode-------------+
! othername ! inode # !
+---------------------+

On the other hand, there's a special file type whose data part carries a path to another file. Since it is a special file, the OS recognizes the data as a path, and redirects opens, reads, and writes so that, instead of accessing the data within the special file, they access the data in the file named by the data in the special file. This special file is called a 'soft link' or a 'symbolic link' (aka a 'symlink').

! filename ! inode # !
+--------------------+
\
.-------> ! permbits, etc ! addresses !
+---------inode-------------+
/
/
/
.----------------------------------------------'
(
'--> !"/path/to/some/other/file"!
+---------data-------------+
/ }
.~ ~ ~ ~ ~ ~ ~ }-- (redirected at open time)
( }
'~~> ! filename ! inode # !
+--------------------+
\
'------------> ! permbits, etc ! addresses !
+---------inode-------------+
/
/
.----------------------------------------------------'
(
'-> ! data ! ! data ! etc.
+------+ +------+
Now, the filename part of the file is stored in a special file of its own along with the filename parts of other files; this special file is called a directory. The directory, as a file, is just an array of filename parts of other files.

When a directory is built, it is initially populated with the filename parts of two special files: the '.' and '..' files. The filename part for the '.' file is populated with the inode# of the directory file in which the entry has been made; '.' is a hardlink to the file that implements the current directory.

The filename part for the '..' file is populated with the inode# of the directory file that contains the filename part of the current directory file. '..' is a hardlink to the file that implements the immediate parent of the current directory.

The 'ln' command knows how to build hardlinks and softlinks; the 'mkdir' command knows how to build directories (the OS takes care of the above hardlinks).

There are restrictions on what can be hardlinked (both links must reside on the same filesystem, the source file must exist, etc.) that are not applicable to softlinks (source and target can be on separate file systems, source does not have to exist, etc.). OTOH, softlinks have other restrictions not shared by hardlinks (additional I/O necessary to complete file access, additional storage taken up by softlink file's data, etc.)

In other words, there's tradeoffs with each.

Now, let's demonstrate some of this...

ln in action
Let's start off with an empty directory, and create a file in it

~/directory $ ls -lia
total 3
73477 drwxr-xr-x 2 lpitcher users 1024 Mar 11 20:16 .
91804 drwxr-xr-x 29 lpitcher users 2048 Mar 11 20:16 ..

~/directory $ echo "This is a file" >basic.file

~/directory $ ls -lia
total 4
73477 drwxr-xr-x 2 lpitcher users 1024 Mar 11 20:17 .
91804 drwxr-xr-x 29 lpitcher users 2048 Mar 11 20:16 ..
73478 -rw-r--r-- 1 lpitcher users 15 Mar 11 20:17 basic.file

~/directory $ cat basic.file
This is a file

Now, let's make a hardlink to the file

~/directory $ ln basic.file hardlink.file

~/directory $ ls -lia
total 5
73477 drwxr-xr-x 2 lpitcher users 1024 Mar 11 20:20 .
91804 drwxr-xr-x 29 lpitcher users 2048 Mar 11 20:18 ..
73478 -rw-r--r-- 2 lpitcher users 15 Mar 11 20:17 basic.file
73478 -rw-r--r-- 2 lpitcher users 15 Mar 11 20:17 hardlink.file

~/directory $ cat hardlink.file
This is a file

We see that:

hardlink.file shares the same inode (73478) as basic.file
hardlink.file shares the same data as basic.file
If we change the permissions on basic.file:

~/directory $ chmod a+w basic.file

~/directory $ ls -lia
total 5
73477 drwxr-xr-x 2 lpitcher users 1024 Mar 11 20:20 .
91804 drwxr-xr-x 29 lpitcher users 2048 Mar 11 20:18 ..
73478 -rw-rw-rw- 2 lpitcher users 15 Mar 11 20:17 basic.file
73478 -rw-rw-rw- 2 lpitcher users 15 Mar 11 20:17 hardlink.file

then the same permissions change on hardlink.file.

The two files (basic.file and hardlink.file) share the same inode and data, but have different file names.

Let's now make a softlink to the original file:

~/directory $ ln -s basic.file softlink.file

~/directory $ ls -lia
total 5
73477 drwxr-xr-x 2 lpitcher users 1024 Mar 11 20:24 .
91804 drwxr-xr-x 29 lpitcher users 2048 Mar 11 20:18 ..
73478 -rw-rw-rw- 2 lpitcher users 15 Mar 11 20:17 basic.file
73478 -rw-rw-rw- 2 lpitcher users 15 Mar 11 20:17 hardlink.file
73479 lrwxrwxrwx 1 lpitcher users 10 Mar 11 20:24 softlink.file -> basic.file

~/directory $ cat softlink.file
This is a file

Here, we see that although softlink.file accesses the same data as basic.file and hardlink.file, it does not share the same inode (73479 vs 73478), nor does it exhibit the same file permissions. It does show a new permission bit: the 'l' (softlink) bit.

If we delete basic.file:

~/directory $ rm basic.file

~/directory $ ls -lia
total 4
73477 drwxr-xr-x 2 lpitcher users 1024 Mar 11 20:27 .
91804 drwxr-xr-x 29 lpitcher users 2048 Mar 11 20:18 ..
73478 -rw-rw-rw- 1 lpitcher users 15 Mar 11 20:17 hardlink.file
73479 lrwxrwxrwx 1 lpitcher users 10 Mar 11 20:24 softlink.file -> basic.file

then we lose the ability to access the linked data through the softlink:

~/directory $ cat softlink.file
cat: softlink.file: No such file or directory

See also

  • For other uses of "ln", see the ln
    LN
    LN may refer to:* Lawful Neutral, an alignment in Dungeons & Dragons* Liaoning, China* Libyan Arab Airlines' IATA code* Louisville and Nashville Railroad's reporting mark* LN postcode area in Britain, named after Lincoln...

     disambiguation page.
  • List of Unix programs
  • NTFS junction point
    NTFS junction point
    An NTFS junction point is a feature of the NTFS file system that provides the ability to create a symbolic link to a directory which then functions as an alias of that directory...


External links


Manual pages

  • ln -- manual page from GNU
    GNU
    GNU is a Unix-like computer operating system developed by the GNU project, ultimately aiming to be a "complete Unix-compatible software system"...

     coreutils
  • ln -- manual page from OpenBSD
    OpenBSD
    OpenBSD is a Unix-like computer operating system descended from Berkeley Software Distribution , a Unix derivative developed at the University of California, Berkeley. It was forked from NetBSD by project leader Theo de Raadt in late 1995...

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK