Linking files and directories: ln

Linking files

To link a file use the command:

   ln filename1 linkname

Making a link to a file does not create another copy of the file; it makes a connection between the file and the linkname

Examples

To link files in the same directory:

   ln notes circular

This creates a link circular to the file notes in the current working directory.

To make several links to a file in different directories:

   ln part1.txt ../helpdata/sect1 /public/helpdoc/part1

This links part1.txt to ../helpdata/sect1 and /public/helpdoc/part1.

To use ln together with special pattern matching characters:

   ln project/*.xdh $PWD

This links all the files with the extension ".xdh" in the sub-directory project to the current directory.

The value of the environment variable PWD is used instead of giving the path to the current directory.

Problems linking files

filename : Cross-device link
The file you are trying to make a link to is located on a different physical device. The solution is to create a symbolic link.
directory_name is a directory
You are trying to make a link to a directory. Check the pathname for the file.
ln: filename : File exists
A file with the same name as the linkname already exists in the destination directory. Use a different name for the linkname.

Using symbolic links

Your files (and directories) may be located on several different file systems. To link files that are in different file systems you need to make a symbolic link.

To make a symbolic link use the command:

   ln -s source linkname

Examples of using the ln -s command

To make a symbolic link from a file in one subdirectory to a file in another subdirectory:

   ln -s reports/reportA publications/my_report

This makes a symbolic link between the file reportA in the subdirectory reports and the filename my_report in the publications subdirectory.

To make a symbolic link to a file in a subdirectory to your current working directory:

   ln -s docs/editors/vi.ref .

This makes a symbolic link between the file vi.ref in the subdirectory docs/editors/ to the filename vi.ref in the current working directory.

Making links to other user's files

To make a link to a file belonging to another user you need to:

  1. know the pathname to the file
  2. have appropriate access permissions to the file

Only the owner can change a file's access permissions.

Linking directories

To link one or more directories to another directory use the command:

   ln -s directory_name(s) directory_name

The use of the -s option indicates that this is to be a symbolic link. As a user you are restricted to creating symbolic links between directories using the -s option.

Examples

To link a directory into your current directory:

   ln -s $HOME/accounts/may

This links the directory $HOME/accounts/may into your current working directory. This link will have the same name may as the directory it points to.

To link several directories into a subdirectory:

   ln -s $HOME/hyperhelp $HOME/metacard experimental

This links the directories $HOME/hyperhelp and $HOME/metacard into the subdirectory experimental.

Understanding the location of linked directories

There is a subtle difference between a "normal" directory and a directory created with a symbolic link to another directory.

What happens if you do a cd (change directory) to this type of directory? You are placed in the directory that the symbolic link points to.

Look at this example:

   pwd

(print the user's working directory)

/usr/home/john/tmp

ln -s $HOME/accounts/may fifth

(use a symbolic link named fifth that points to

the directory $HOME/accounts/may)

ls

fifth

cd fifth

(change directory to the new directory)

pwd

(print the working directory)

/usr/home/john/accounts/may

(the user is actually in the the directory that the

linked directory points to)

When the user does a cd (change directory) to the symbolically linked directory may in their current working directory, they change to the directory that this link points to, the directory /usr/home/john/accounts/may.