ln

Make a link to a file.

Format

   ln [options] existing-file new-link
   ln [options] existing-file-list directory

Summary

By default, ln makes hard links. A hard link to a file is indistinguishable from the original filename. You can refer to the file either by its original filename or by the name given to it by the ln command, and in either case the effects will be the same. All hard links to a file must be in the same file system as the original file. You can't create a hard link on hopper to a file located on turing, or vice versa.

When you are using ln, you can use the first version shown above to create a link between an existing file and a new filename. You can use the second format to link existing files into a different directory. The new links will have the same simple filenames as the original files but different full pathnames.

You can use ln to create symbolic links as well as hard links. Unlike a hard link, a symbolic link can exist in a different file system than the linked-to file. If your account is on hopper, you can create a symbolic link to a file located on turing, or vice versa. For that reason, you should normally use symbolic links in CSCI 241.

Arguments

The existing-file is the pathname of the file you want to make a link to. The new-link is the pathname of the new link. When you are making a symbolic link, the existing-file may be a directory; otherwise, it cannot be a directory.

Using the second format, the existing-file-list contains the pathnames of the ordinary files you want to make links to. The ln utility establishes the new links so that they appear in the directory. The simple filenames of the entries in the directory are the same as the simple filenames of the files in the existing-file-list.

Options

-s symbolic link This option causes ln to create a symbolic link. When you use this option, the existing-file and new-link may be directories.

Notes

A hard link is an entry in a directory that points to a file. The operating system makes the first link to a file when you create the file using an editor, a program, or redirected output. You can make additional links using ln and remove links using rm. When the last hard link to a file is removed, the file is deleted from the system. The ls utility, with the -l option, shows you how many hard links a file has.

You can use symbolic links to link across file systems and to create links to directories. Creating or deleting a symbolic link to a file does not affect its link count. If the file your symbolic link points to is removed, you'll end up with a symbolic link that points nowhere. When you use the ls -l command to list information about a symbolic link, ls displays -> and the name of the linked-to file after the name of the link.

Examples

The following command makes a symbolic link to the file /home/turing/t90kjm1/CS241/Data/Spring2020/Assign2/employees in the working directory. The ls -l command shows the linked-to filename.

z123456@turing:~/CS241/Assign2$ ln -s ~t90kjm1/CS241/Data/Spring2020/Assign2/employees .
z123456@turing:~$ ls -l
total 112
lrwxrwxrwx 1 z123456 csci    61 2011-01-10 14:29 employees2 -> /home/turing/t90kjm1/CS241/Data/Spring2010/Assign2/employees2
-rw-r--r-- 1 z123456 csci   325 2010-12-11 04:13 prog2.cpp
-rwxr-xr-x 1 z123456 csci 42533 2010-12-11 04:14 prog2
-rw-r--r-- 1 z123456 csci 57160 2010-12-11 04:14 prog2.o
z123456@turing:~$