DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH
 

ln(C)


ln -- make a link to a file

Syntax

ln [ -s ] [ -f ] sourcefile targetfile

ln [ -s ] [ -f ] sourcefile ... targetdirectory

Description

A link is a directory entry referring to a file; a single file (together with its size, all its protection information, and so on) may have several links to it. There are two kinds of link: hard links and symbolic links.

By default ln makes hard links. A hard link to a file is indistinguishable from the original directory entry; any changes to a file are effective independent of the name used to reference the file. Hard links may not span filesystems and may not refer to directories.

The -s option causes ln to create symbolic links. A symbolic link contains the name of the file to which it is linked; this file does not need to exist prior to the symbolic link. Symbolic links may span file systems and may refer to directories.

When ln is given two arguments, and the second argument does not exist, ln creates a directory entry called targetfile that is a link to sourcefile.

If the last argument is the name of a directory, ln creates a new entry in the directory for each sourcefile given. The name of each new entry will be the basename of the corresponding sourcefile.

If the -f option is not specified and the target filename already exists, the link is not created. If the -f option is specified, the existing file is unlinked before the new link is created.

If you try to create a symbolic link to a file in your current working directory using a relative pathname, you will not achieve the desired result. In this example

touch sln
ln -s sln /tmp/sln

you are creating a file /tmp/sln that refers to a file sln in the /tmp directory: that is, you are creating a self-referential file in /tmp rather than a reference to the file sln in the current working directory.

A listing of the file /tmp/sln produces the following output:

   lrwxrwxrwx  1  user1  group1   2 Sep 07 10:35 /tmp/sln -> sln
If you then try to open the file using a command such as vi, an error will be returned because the symbolic link ends up ``chasing its own tail''. You cannot avoid this situation by entering the command:

ln -s ./sln /tmp/sln

rather, you should use the full pathname:

ln -s /user1/sln /tmp/sln

A listing of /tmp/sln will then produce this output:

   lrwxrwxrwx  1  user1  group1   2 Sep 07 10:35 /tmp/sln -> /user1/sln
The symbolic link correctly references the file in the current working directory.

Exit values

ln returns the following values:

0
all of the specified files were linked successfully

>0
an error occurred

Limitations

Only EAFS, DTFS(TM) and HTFS(TM) support symbolic links. If you try ln -s on other filesystems, you see the error message: Cannot make symbolic link.

See also

cp(C), divvy(ADM), mv(C), rm(C)

Standards conformance

ln is conformant with:

ISO/IEC DIS 9945-2:1992, Information technology - Portable Operating System Interface (POSIX) - Part 2: Shell and Utilities (IEEE Std 1003.2-1992);
AT&T SVID Issue 2;
X/Open CAE Specification, Commands and Utilities, Issue 4, 1992.


© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003