Ln (Unix)

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 links, which refer to a symbolic path indicating the abstract location of another file, and
*hard links, 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 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:

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 in inode 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 a broken 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 a hard 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 from OpenBSD


Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать реферат

Look at other dictionaries:

  • Unix — (officially trademarked as UNIX, sometimes also written as Unix with small caps) is a computer operating system originally developed in 1969 by a group of AT T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Douglas McIlroy, and… …   Wikipedia

  • UNIX — Ken Thompson und Dennis Ritchie Basisdaten Entwickler …   Deutsch Wikipedia

  • Unix — Kommandozeile (Unix Prompt) Basisdaten Entwickler Ken Thompso …   Deutsch Wikipedia

  • Unix — (registrado oficialmente como UNIX®) es un sistema operativo portable, multitarea y multiusuario; desarrollado, en principio, en 1969 por un grupo de empleados de los laboratorios Bell de AT T, entre los que figuran Ken Thompson, Dennis Ritchie y …   Wikipedia Español

  • UNIX — UNIX, parfois écrit « Unix », avec des petites capitales, est un système d exploitation multitâche et multi utilisateur créé en 1969, conceptuellement ouvert et fondé sur une approche par laquelle il offre de nombreux petits outils… …   Wikipédia en Français

  • Unix — (marque déposée officiellement comme UNIX, parfois aussi écrit comme Unix avec les petites capitalisations) est le nom d un système d exploitation multitâche et multi utilisateur créé en 1969, conceptuellement ouvert et fondé sur une approche par …   Wikipédia en Français

  • Unix time — Unix time, or POSIX time, is a system for describing points in time, defined as the number of seconds elapsed since midnight Coordinated Universal Time (UTC) of January 1 1970, not counting leap seconds. It is widely used not only on Unix like… …   Wikipedia

  • Unix-подобная операционная система — Генеалогическое дерево UNIX подобных ОС UNIX подобная операционная система (иногда сокр. *nix)  система, которая образовалась под влиянием UNIX. Термин включает свободные/открытые операционные системы, образованные от UNIX компании …   Википедия

  • UNIX — 〈[ju:nıx] EDV〉 Betriebssystem für Computer, das zum großen Teil in der Programmiersprache C geschrieben ist [engl.] * * * UNIX   [ursprünglich UNICS, Abk. für Uniplexed Information and Computing System, dt. »nicht multiplextes (vielseitiges)… …   Universal-Lexikon

  • UNIX-Kommandos — Unix Systeme zeichnen sich durch eine Vielzahl von Kommandos aus, mit denen sich über eine Shell das Betriebssystem bedienen lässt. Die Syntax dieser Kommandos weicht unter den verschiedenen Systemen voneinander ab. Es existieren die beiden… …   Deutsch Wikipedia

  • Unix-Befehle — Unix Systeme zeichnen sich durch eine Vielzahl von Kommandos aus, mit denen sich über eine Shell das Betriebssystem bedienen lässt. Die Syntax dieser Kommandos weicht unter den verschiedenen Systemen voneinander ab. Es existieren die beiden… …   Deutsch Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”