ln(1) ln(1)
NAME
ln - make links to files
SYNOPSIS
ln [option] source target Format 1
ln [option] source ... targdir Format 2
ln -s [--] source target Format 3
ln -s [--] source ... targdir Format 4
DESCRIPTION
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.
OPTIONS
Format 1: Make a hard link
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.
Page 1 Reliant UNIX 5.44 Printed 11/98
ln(1) ln(1)
No option specified:
If a file with the same name as that to be assigned to the link
to be created already exists, ln returns an error message. The
existing file is not overwritten.
This corresponds to the old option -n, which will continue to be
supported for compatibility reasons.
-f (force) If a file named target already exists, ln creates the
link without asking questions. If link was the only link to the
original file, the contents of the file are deleted after ln is
executed.
-- If source begins with a dash (-), the end of the command-line
options must be marked with --.
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 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).
Page 2 Reliant UNIX 5.44 Printed 11/98
ln(1) ln(1)
Format 2: Make a hard link with the same name in another directory
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 dif-
ferent directories under the same basic file name (basename).
No option specified:
If a file with the same name as that to be assigned to the link
to be created already exists, ln returns an error message. The
existing file is not overwritten.
This corresponds to the old option -n, which will continue to be
supported for compatibility reasons.
-f (force) If targdir already contains a file named source, ln
creates the link without asking questions, If link was the only
reference to the original file, the contents of the file are
deleted after ln is executed.
-- If source begins with a dash (-), the end of the command-line
options must be marked with --.
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 rela-
tive 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.
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 dif-
ferent file systems.
Page 3 Reliant UNIX 5.44 Printed 11/98
ln(1) ln(1)
Format 3: Make a symbolic link
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.
-- If source begins with a dash (-), the end of the command-line
options must be marked with --.
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 MESSAGES). The existing file is not overwrit-
ten.
Format 4: Make a symbolic link with the same name in another directory
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.
-- If source begins with a dash (-), the end of the command-line
options must be marked with --.
source
Name of the file or directory to which you want to make a sym-
bolic link. You can name any number of sources in one call.
The specified source can be an absolute or relative path name:
Page 4 Reliant UNIX 5.44 Printed 11/98
ln(1) ln(1)
an absolute or relative path name in the form prefix/name:
ln enters the basename name in the directory targdir as a sym-
bolic link to prefix/name.
If targdir already contains a file with the same basename as
name, an error is returned (see ERROR MESSAGES), 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) associ-
ated 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 direc-
tory 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 sym-
bolic 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 res-
tricted by file system boundaries, and the path names contained in
them may refer both to files and to directories.
Page 5 Reliant UNIX 5.44 Printed 11/98
ln(1) ln(1)
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 sym-
bol "->".
- ls -L provides information on the file or directory that is refer-
enced 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 MESSAGES
ln: no permission for link
ln cannot create the named link, since you do not have write permis-
sion for the directory in which the link is to be entered.
ln: cannot access file
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> directory
You have specified the directory dir instead of a file. ln only estab-
lishes hard links to regular files, special files, or FIFO files, not
to directories.
ln: different file system
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 file
ln: File exists
You have tried to create a link named file in a directory, but there
is already a file under the same name in that directory.
LOCALE
The LCMESSAGES environment variable governs the language in which
message texts are displayed. If LCMESSAGES is undefined or is defined
as the null string, it defaults to the value of LANG. If LANG is like-
wise undefined or null, the system acts as if it were not internation-
alized.
Page 6 Reliant UNIX 5.44 Printed 11/98
ln(1) ln(1)
Answers to yes/no queries must be given in the language appropriate to
the current locale.
The LCALL environment variable governs the entire locale. LCALL
takes precedence over all the other environment variables which affect
internationalization.
EXAMPLES
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 condi-
tions 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
Page 7 Reliant UNIX 5.44 Printed 11/98
ln(1) ln(1)
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
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 3
User Norbert has created the directories letters89, letters90 and
letters91 in his home directory /home/norbert:
$ ls /home/norbert
letters89 letters90 letters91
He now wants direct access from /home/norbert/letters91 to the other
two directories. To do this, he creates two symbolic links by calling
ln in Format 4:
$ cd /home/norbert
$ ln -s /home/norbert/letters89 /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
Page 8 Reliant UNIX 5.44 Printed 11/98
ln(1) ln(1)
SEE ALSO
chmod(1), cp(1), find(1), ls(1), mv(1), pax(1), rm(1), link(2),
readlink(2), stat(2), symlink(2).
Page 9 Reliant UNIX 5.44 Printed 11/98