A Quick Introduction to Unix/Links


Links edit

There are times when it may be helpful to have two different names that refer to the same file. For example, a file may have a very long name that cannot be changed because it is needed for identification purposes. For ease of use, you could also refer to the file using a shorter name. These different references to the same file are called links, and there are two different kinds of links: hard links and soft links.

Hard Links edit

A hard link is a name that directly references a file on the file system. Most filenames are hard links: they refer to a file system location on a storage medium. More than one filename can refer to that file system location. If file1 and file2 are both hard links that refer to a file that contains text, any editing saved in file1 will show up when you look at file2. Hard links are independent, so if you remove file1, file2 still refers to the file on the filesystem.

Soft Links edit

A soft link (also called a Symbolic link) is a name that references a filename (not a file). If file1 is the name of a file in your home directory (a hard link to the file on the file system) and file2 is a soft link, it refers not to the file on the file system, but to file1. Anything saved in file1 will still show up in file2, but if file1 is removed, file2 will no longer refer to a valid filename.

ln (link) edit

The Unix command to create links is ln. By default, the ln command creates hard links. The option -s tells the command to create a soft link instead.

Let's create a file and then create a hard link and a soft link.

% touch file.txt
% ls -l
% ln file.txt hardlink.txt 
% ln -s file.txt softlink.txt
% ls -l