- Ln (Unix)
ln is a standard
Unix command 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 s, which refer to a symbolic path indicating the abstract location of another file, and
*hard link 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.
pecification
The
Single Unix Specification (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.
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.
Other Unix and
Unix-like 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 names of each files before links are created, and others. BSD ln adds -h, preventing ln from descending into targets whose symlinks point to directoriesln 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:
To begin with, enter the directory called "test": alex@ubuntu:~/playground/test$ ls -ali total 8 969797 drwxr-xr-x 2 alex alex 4096 Dec 9 09:10 . 1036602 drwxr-xr-x 3 alex alex 4096 Dec 9 09:10 .. alex@ubuntu:~/playground/test$There is nothing in it. Let's create a small text file called data.txt: alex@ubuntu:~/playground/test$ echo "some data" > data.txt alex@ubuntu:~/playground/test$ ls -ali total 12 969797 drwxr-xr-x 2 alex alex 4096 Dec 9 09:11 . 1036602 drwxr-xr-x 3 alex alex 4096 Dec 9 09:10 .. 969768 -rw-r--r-- 1 alex alex 10 Dec 9 09:11 data.txt alex@ubuntu:~/playground/test$Notice that the
reference count of the directory (total 8) just increased (total 12). The new file data.txt is stored ininode 969768.Let's create a symbolic (soft) link to data.txt. We will name our symbolic link slink.txt: alex@ubuntu:~/playground/test$ ln -s data.txt slink.txt alex@ubuntu:~/playground/test$ ls -ali total 12 969797 drwxr-xr-x 2 alex alex 4096 Dec 9 09:11 . 1036602 drwxr-xr-x 3 alex alex 4096 Dec 9 09:10 .. 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 alex@ubuntu:~/playground/test$The reference count of the directory has not changed (total 12). Our 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: alex@ubuntu:~/playground/test$ file slink.txt slink.txt: symbolic link to `data.txt' alex@ubuntu:~/playground/test$ cat slink.txt some data alex@ubuntu:~/playground/test$If we delete the text file data.txt, slink.txt becomes abroken link and our data is lost. alex@ubuntu:~/playground/test$ rm data.txt alex@ubuntu:~/playground/test$ ls -ali total 8 969797 drwxr-xr-x 2 alex alex 4096 Dec 9 09:36 . 1036602 drwxr-xr-x 3 alex alex 4096 Dec 9 09:32 .. 969817 lrwxrwxrwx 1 alex alex 8 Dec 9 09:11 slink.txt -> data.txt alex@ubuntu:~/playground/test$ file slink.txt slink.txt: broken symbolic link to `data.txt' alex@ubuntu:~/playground/test$ cat slink.txt cat: slink.txt: No such file or directory alex@ubuntu:~/playground/test$If slink.txt was ahard link , our data would still be accessible through slink.txt.Also, if you delete the soft link itself, the data would still be there:$ ls -ali
63500 drwxr-xr-x 2 sc69876 support 96 Aug 29 18:14 . 43038 drwxr-xr-x 12 sc69876 support 1024 Aug 29 18:13 .. 104690 -rw-r--r-- 1 sc69876 support 10 Aug 29 18:13 data.txt 104825 lrwxrwxrwx 1 sc69876 support 8 Aug 29 18:14 slink.txt -> data.txt$ rm slink.txt
$ ls -ali
63500 drwxr-xr-x 2 sc69876 support 96 Aug 29 18:15 . 43038 drwxr-xr-x 12 sc69876 support 1024 Aug 29 18:13 .. 104690 -rw-r--r-- 1 sc69876 support 10 Aug 29 18:13 data.txt
ee also
*
NTFS junction point
*List of Unix programs External links
* [http://www.opengroup.org/onlinepubs/009695399/utilities/ln.html ln] -- specification from the Single Unix Specification
* [http://en.wikibooks.org/wiki/Guide_to_UNIX/Commands/File_System_Utilities#ln Simple guide to ln]Manual pages
* [http://www.gnu.org/software/coreutils/manual/html_node/ln-invocation.html ln] -- manual page from
GNU coreutils
* [http://www.openbsd.org/cgi-bin/man.cgi?query=ln ln] -- manual page fromOpenBSD
Wikimedia Foundation. 2010.