Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

ln - link files

&pagelevel(4)&pagelevel

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



Format 1: ln[ option] source target


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!
If the standard input is not a terminal, no confirmation is requested and the link is not made.


option

-f

(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.

-n

If a file named target already exists, its contents are not overwritten.

The -f option overrides this option.

source

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:
ln enters the basename target in the current working directory.

an absolute or relative path name in the form prefix/name:
ln enters the basename name in the directory identified as prefix.

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 you do not have write permission for the original target, ln asks you if you really want it to make the link (see option -f and Example 2).

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:

  • the file must be owned by the user

  • the directory must be owned by the user

  • the user must have write permission for the file

  • the user must be a privileged user.


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
Format 2: ln[ option] source ... targdir


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!
If the standard input is not a terminal, no confirmation is requested and no link is created.


option

-f

(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.

-n

If a file named source already exists in targdir, its contents are not overwritten.

The -f option overrides this option.

source

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:
ln enters the basename source in the named targdir.

an absolute or relative path name in the form prefix/name:
ln enters the basename name in the named targdir.

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).

targdir

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.

Format 3: ln -s source target


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.

source

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.

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:
ln enters the basename target as a symbolic link in the current working directory.

an absolute or relative path name in the form prefix/name:
ln enters the basename name in the directory identified as prefix.

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
Format 4: ln -s source ... targdir


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.

source

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.
The specified source can be an absolute or relative path name:

an absolute or relative path name in the form prefix/name:
ln enters the basename name in the directory targdir as a symbolic link to 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.

targdir

Name of the directory in which the symbolic links are to be created. This directory must exist.

Functionality

  • Hard links

    When ln makes a link to a file, the basic file name (basename) associated with the link is entered in the appropriate directory. This entry receives the same inode number as the original file name. Thus, both file names have the same inode, and consequently the same attributes (access permissions, owner, dates, etc.). The physical file referenced by the file names is only present once, but the user can now access the same file under different file or path names (see Example 1).

    The inode number indicates whether two file names are linked to the same file
    (see ls -i); the number of links shows you how many directory entries exist for the file (see ls -l).

    The rm command can be used to remove an entry (link) from a directory. If there was more than one link to the file, it can still be accessed under the remaining names. rm does not delete the file itself until the last link (i.e. name) is removed.

  • Symbolic links
    A symbolic link is a file that contains a path name. These path names can be listed with the command ls -l. When the shell encounters a file name that represents a symbolic link, it replaces this name with the specified path name. In other words, one path name is mapped to another. There is no link counting mechanism in this case; when a symbolic link is removed, the file containing the path name is deleted.
    The inode of the physical file that is referenced by a symbolic link does not contain any information about the link. The link counter only keeps track of hard links. This means that if you delete the target of a symbolic link, the link will continue to exist, but will now refer to a file having no contents and no inode.
    Symbolic links are not restricted by file system boundaries, and the path names contained in them may refer both to files and to directories.
    Symbolic links can be shown with the ls command:
    • ls -l indicates which files in the specified directory are symbolic links. The contents of each symbolic link, i.e. the path name of the referenced file, are shown following the file name and the symbol ’->’.
    • ls -L provides information on the file or directory that is referenced by the symbolic link.

Operations involving ’..’ (such as ’cd ..’) in a directory that is symbolically linked will reference the original directory, not the target.

Error

ln: Cannot link <source> to <target>: Permission denied

ln cannot create the named link, since you do not have write permission for the directory in which the link is to be entered.


ln: cannot access <file>: no such file or directory
ln cannot access the named file, either because it does not exist or because you have no execute permission (x bit) for a directory that appears in the path name of file.


ln: <dir> is a directory
You have specified the directory dir instead of a file. ln only establishes hard links to regular files, special files, or FIFO files, not to directories.


ln: are on different file systems

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.


ln: cannot create
ln: File exists

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:

  • write permission for her own directory /HOME/MARY

  • execute permission for Pat’s directory /HOME/PAT:

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 fastest way for Mary to set up the links is to call ln in Format 2:

$ ln /HOME/PAT/frogs /HOME/PAT/snails /HOME/MARY

The same result can be achieved by calling ln twice in Format 1:

$ ln /HOME/PAT/frogs /HOME/MARY/frogs

$ ln /HOME/PAT/snails /HOME/MARY/snails

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.

$ ls -l /HOME/MARY
total 6

-rw-rw----   2 PAT      proj          34   Mar 09 15:08 frogs

-rw-rw----   2 PAT      proj        1217   Mar 09 18:59 snails

$ ls -i /HOME/PAT/frogs /HOME/MARY/frogs

16435 /HOME/PAT/frogs

16435 /HOME/MARY/frogs

$ ls -i /HOME/PAT/snails /HOME/MARY/snails

 4766 /HOME/PAT/snails

 4766 /HOME/MARY/snails

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:

$ chmod 600 frogs snails

If Mary wants to create links with different names, she has to call ln twice in Format 1.

$ ln /HOME/PAT/frogs /HOME/MARY/sugar

$ ln /HOME/PAT/snails /HOME/MARY/spice

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:

$ ls -l

total 3

-rw-r--r--   1  BRIAN    other     57  Jul 17 14:29 letter
-r--r--r--   2  NEIL     other    103  Jul 16 15:30 list

-r--r--r--   2  NEIL     other    103  Jul 16 15:30 text

You now enter:

$ ln letter list

ln: list: 444 mode (y/n) ?n


You answered n, so ln does not make the link.

If you have write permission for file2, ln makes the link without asking questions:

$ ls -l

total 3

-rw-r--r--   1  BRIAN    other     57  Jul 17 14:29 letter
-rw-rw-rw-   2  NEIL     other    103  Jul 16 15:30 list

-rw-rw-rw-   2  NEIL     other    103  Jul 16 15:30 text

$ ln letter list
$ ls -l

total 3

-rw-r--r--   2  BRIAN    other     57  Jul 17 14:29 letter
-rw-r--r--   2  BRIAN    other     57  Jul 17 14:29 list

-rw-rw-rw-   1  NEIL     other    103  Jul 16 15:30 text

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.

$ ln letter text
$ ls -l

total 3

-rw-r--r--   3  BRIAN    other     57  Jul 17 14:29 letter
-rw-r--r--   3  BRIAN    other     57  Jul 17 14:29 list

-rw-r--r--   3  BRIAN    other     57  Jul 17 14:29 text

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:

$ ln -s /HOME2/ANDREA /HOME/NORBERT/andr

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:

$ ls -lL andr

total 16

drwxr-xr-x   2 ANDREA   usrother    2560 Feb 27 13:20 PASCAL

drwxr-xr-x   2 ANDREA   usrother    2048 Mar  5 17:32 COURSES

drwxr-xr-x   2 ANDREA   usrother     512 Mar  5 12:07 LETTERS
drwxr-xr-x   2 ANDREA   usrother     512 Feb 26 10:05 bin

-rw-r-xr-x   1 ANDREA   usrother     148 Feb 20 09:28 testprog

Example 4

User Norbert has created the directories letters89, letters90 and letters91 in his home directory /HOME/NORBERT:

$ ls /HOME/NORBERT

letters 89 letters 90 letters 91

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:

$ cd /HOME/NORBERT

$ ln -s /HOME/NORBERT/letters 89 /HOME/NORBERT/letters90 letters91

$ cd letters91
$ ls -og letters??

lrwxrwxrwx   1     25 Mar  6 12:42 letters89 -> /HOME/NORBERT/letters89

lrwxrwxrwx   1     25 Mar  6 12:42 letters90 -> /HOME/NORBERT/letters90
$ ls letters90

offers      letter.henry  letter.nigel  text

See also

chmod, cp, ls, mv, rm
link(), readlink(), stat(), symlink() [4]