Ln (Unix)
From Wikipedia, the free encyclopedia
- The correct title of this article is ln (Unix). The initial letter is shown capitalized due to technical restrictions.
ln is a standard Unix program used to create links (link) to files.
Contents |
[edit] Link files
Links allow more than one file 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.
[edit] Specification
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.
[edit] Usage
The SUS mandates for ln two options: -f, which will force removal of existing files to allow the link to be created, and -s, to specify symbolic links.
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
[edit] Examples
ln xyz abc
would have the effect of creating a hard link called abc that points to the same data as the existing file xyz.
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.
[edit] See also
[edit] External links
- ln -- specification from the Single Unix Specification
[edit] Manual pages
Unix command line programs and builtins (more) | |||
File and file system management: | cat | cd | chmod | chown | chgrp | cp | du | df | file | fsck | ln | ls | lsof | mkdir | mount | mv | pwd | rm | rmdir | split | touch | ||
Process management: | at | chroot | crontab | exit | kill | killall | nice | pgrep | pidof | pkill | ps | sleep | time | top | wait | watch | ||
User Management/Environment: | env | finger | id | mesg | passwd | su | sudo | uname | uptime | w | wall | who | whoami | write | ||
Text processing: | awk | comm | cut | ex | head | iconv | join | less | more | paste | sed | sort | tail | tr | uniq | wc | xargs | ||
Shell programming: | echo | expr | printf | unset | Printing: | lp |
Communications: inetd | netstat | ping | rlogin | traceroute |
Searching: find | grep | strings |
Miscellaneous: banner | bc | cal | man | size | yes |