.. include:: ../header.txt .. _links: Reference Files with Links =============================== .. _ln: :command:`ln` --------------- :title:`Linux for Programmers and Users`, Section 4.9 .. index:: ln .. program:: ln .. describe:: ln Create both hard and soft links to files. SYNOPSIS :command:`ln` [-s] TARGET [LINK_NAME] DESCRIPTION Create a link to the specified TARGET with optional LINK_NAME. If LINK_NAME is omitted, a link with the same basename as the TARGET is created in the current directory. .. option:: -s, --symbolic make symbolic links instead of hard links A hard link makes an additional reference (link) to the inode for the file. A symbolic link is a pointer to the path name of the file. .. index:: inodes .. _inodes: Background - Inodes ----------------------- To understand hard links, we first need to understand a few things about Unix file systems. Each file system contains a table of inodes, which are data structures. Each inode contains information about one file information about each file. The directory tree hierarchy is an abstraction created by special files called directories. A directory is actually a file with names of files and a pointer to an inode number for each file. .. figure:: inodes.png :align: center An inode is a data structure with information about the file such as the ownership, permissions, timestamps and it holds pointers to where the data for the file is located at on the drive. About the only information about the file that is not in the inode is the file's name. That is contained in the directory file. To see the inode number of a file use the :option:`ls -i` command. The second field in the output from :option:`ls -l` (third with the `-i` option) is *link count*. It corresponds to number of files (items in directory files) that reference the inode. Hard Links ------------- When a hard link is created, a new file item in the directory is created that lists the inode of the TARGET file. The link count of the inode is also incremented. Whenever a file is deleted with the :ref:`rm` command, the link count is decremented. Then, only if the new link count is zero, is the inode freed for use by other files that might be created later. Consider the following sequence of commands:: $ ls -il f1 1393387 -rw-rw-r-- 1 tim tim 227 Sep 30 14:19 f1 $ ln f1 f2 $ ls -il f1 f2 1393387 -rw-rw-r-- 2 tim tim 227 Sep 30 14:19 f1 1393387 -rw-rw-r-- 2 tim tim 227 Sep 30 14:19 f2 $ rm f1 $ ls -il f2 1393387 -rw-rw-r-- 1 tim tim 227 Sep 30 14:19 f2 Advantages of Hard Links ^^^^^^^^^^^^^^^^^^^^^^^^^ * *Backup*: Prevention from accidental deletion. * Allows the same file to be executed as two similar but separate programs. * Takes care of old programs that accesses a file whose name or location has changed. Limitations of Hard Links ^^^^^^^^^^^^^^^^^^^^^^^^^^ * Can't link directories. * Can't link across file systems. Symbolic Links ---------------- * :option:`ln -s` * Separate file type and having its own inode. In output of :option:`ls -l`, the first character for a symbolic link is `l`. * Contains the pathname of another file or directory. If the target is later moved, the link is said to *stale* or *broken*. * Can link across file systems. * Link and file pointed to are not equivalent. * One advantage of symbolic links, is that the output of :option:`ls -l`, displays the path location of the TARGET file. :: $ mkdir dir $ cp f1 dir $ ln -s f1 f2 $ ln -s dir/f1 f3 $ ls -il f1 f2 f3 1393387 -rw-rw-r-- 1 tim tim 227 Sep 30 14:19 f1 1393389 lrwxrwxrwx 1 tim tim 2 Oct 7 11:00 f2 -> f1 1393390 lrwxrwxrwx 1 tim tim 6 Oct 7 11:00 f3 -> dir/f1