ln creates links to existing files or directories. This allows you to access these files and/or directories under various file names or path names (see Functionality).
To make a link with ln, you need write permission for the directory in which the link is to be entered.
There are two types of link:
Hard link:
When you create a hard link to a file, there are multiple directory entries for this file, either in one directory or in different directories, but the physical file is only present once. The inode for each file contains a link counter which is decremented by one when a link is removed. The file itself is not deleted until all links to it have been removed.
A hard link cannot be used to refer to a directory or to files on different file systems.Symbolic link:
A symbolic link is a file that contains a path name. When the shell encounters a file name that represents a symbolic link, it replaces this name with the specified path name. Thus what you access is not the symbolic link, but the file to which the path name points.
You can set up symbolic links to any files or directories, even to those on different file systems.
Syntax
Format 1: |
ln[ option] source target |
Format 2: |
ln[ option] source ... targdir |
Format 3: |
ln -s source target |
Format 4: |
ln -s source ... targdir |
Make a hard link
ln makes a new link named target to the file named source. The file can then be accessed under both names, i.e. source and target. No option specified If there is already a file with the same name as the new link and you do not have write permission for it, ln will display its access permissions and ask you to confirm whether or not the link should be made. The link will not be created unless your answer begins with y (see target and Example 2). Caution! option
(force) If a file named target already exists, ln creates the link without asking questions, regardless of whether or not you have write permission for the file.
If a file named target already exists, its contents are not overwritten. The -f option overrides this option.
Name of the file to which you want to make a link. The file must already exist when you call ln. You are not allowed to specify a directory as the source. target Name of the link that you wish to make to source. target can be a simple file name (basename) or an absolute or relative path name. a basename: an absolute or relative path name in the form prefix/name: If there is already a file named target and you have write permission for it, ln creates the link without asking questions. In other words, the name target now references source, and not the file originally called target; and if target was the only link to (i.e. name of) the original file, the contents of the original file are deleted. If the parent directory of target is writable but has the sticky bit set, one of the following conditions must be fulfilled in order to for target to be created:
This format of ln cannot be used to create links that span different file systems. Such links can be made using the -s option (see Format 3 and Format 4). Make a hard link with the same name in another directory
ln creates a link to the file (or files) named source in another directory named targdir. The file can then be accessed in two different directories under the same basic file name (basename). No option specified If there is already a file with the same name as the new link to be created, and if you do not have write permission for it, ln will display its access permissions and ask you to confirm whether or not the link should be made. The link will not be created unless your answer begins with y. Caution!
(force) If targdir already contains a file named source, ln creates the link without asking questions, regardless of whether or not you have write permission for it.
If a file named source already exists in targdir, its contents are not overwritten. The -f option overrides this option.
Name of the file to which you want to make a link. The file must already exist when you call ln. You are not allowed to make links to directories. You can name any number of files in one call. The specified source can be a file basename or an absolute or relative path name: a basename: an absolute or relative path name in the form prefix/name: If there is already a file with the basename source in targdir and you have write permission for it, ln creates the link without asking questions. In other words, the link now references source, and not the file in targdir originally called source; and if source was the only link to (i.e. name of) the original file, the contents of the original file are deleted. If you do not have write permission for the original file in targdir, ln asks you if you really want it to make the link (see option -f and Example 2).
Name of the directory in which the link is to be entered. The directory must already exist. This format of ln cannot be used to create links that span different file systems. Make a symbolic link
ln -s makes a symbolic link named target to the named source, where source can be a file or a directory. The main difference with ln -s is that it creates symbolic links, which can span different file systems. This is not possible with hard links.
Name of the file or directory to which a symbolic link is to be made. source may be specified as any path name and need not exist. It may also reside on a different file system from target.
Name of the symbolic link that you wish to make to source. target can be a file basename or an absolute or relative path name: a basename: an absolute or relative path name in the form prefix/name: If a file named target already exists, an error message is returned (see Error). The existing file is not overwritten. Make a symbolic link with the same name in another directory
For each file or directory that is specified as source, ln creates a symbolic link in the directory targdir. These links can span different file systems.
Name of the file or directory to which you want to make a symbolic link. You can name any number of sources in one call. an absolute or relative path name in the form prefix/name: If targdir already contains a file with the same basename as name, an error is returned , and the existing file is not overwritten.
Name of the directory in which the symbolic links are to be created. This directory must exist. |
Functionality
Operations involving ’..’ (such as ’cd ..’) in a directory that is symbolically linked will reference the original directory, not the target. |
Error
ln cannot create the named link, since you do not have write permission for the directory in which the link is to be entered.
You have tried to create a link to a file and enter it in a different file system. ln does not create hard links across file systems; try using the -s option.
You have tried to use the -s option to create a link named file in a directory, but there is already a file under the same name in that directory. |
Locale
The following environment variables affect the execution of ln: LANG Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined. LC_ALL If set to a non-empty string value, override the values of all the other internationalization variables. LC_CTYPE Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments and input files), the classification of characters as upper- to lower-case, and the mapping of characters from one case to the other. LC_MESSAGES Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error. NLSPATH Determine the location of message catalogs for the processing of LC_MESSAGES. |
Example 1
User Pat is the owner of the files frogs and snails, both of which are in the directory /HOME/PAT. Mary is to now work with these files as well. To continue working in her own directory and save herself the bother of entering long path names, Mary establishes links to these files in her own directory /HOME/MARY. To be able to do this, she has to have:
To be able to read and modify the files, Mary also needs read and write permission for the files themselves. In our example these conditions are met if Mary is a member of the group proj (see below).
The same result can be achieved by calling ln twice in Format 1:
As is evident from the following ls calls, ln has entered the file names frogs and snails in the directory /HOME/MARY. The number of links to the files has been incremented by 2 (ls -l). The ls -i calls show that the file names frogs and snails have the same inode numbers in both directories.
The ls -l call above shows that Pat is still the owner of the files. Pat can at any time cancel Mary’s right to use the files, e.g. by saying:
If Mary wants to create links with different names, she has to call ln twice in Format 1.
|
Example 2
The following examples shows you what happens if you call ln in Format 1 when there isalready a file with the chosen link name. In the current working directory there are three file names: letter, list and text. letter is a link to file1, list and text are links to file2. You do not have write permission for file2:
You now enter:
You answered n, so ln does not make the link. If you have write permission for file2, ln makes the link without asking questions:
As ln has created a link named list for letter (file1), list is now a link to file1, and no longer to file2. text is now the only link to file2.
Now text is also a link to file1; file2 no longer exists. |
Example 3
User Norbert needs a quick and simple way to access the directory of his colleague Andrea (/HOME2/ANDREA) from his own directory (/HOME/NORBERT). He therefore creates the following symbolic link by calling ln in Format 3:
Norbert can now use the symbolic link andr to directly access Andrea’s directory from his own directory, even though her directory resides on another file system:
|
Example 4
User Norbert has created the directories letters89, letters90 and letters91 in his home directory /HOME/NORBERT:
He now wants direct access from /HOME/NORBERT/letters91 to the other two directoris. To do this, he creates two symbolic links by calling ln in Format 4:
|
See also
chmod, cp, ls, mv, rm link(), readlink(), stat(), symlink() [4] |