UNIX Primer

Home | Primer Home


Links

A link is a special type of file that refers to another file. There are two types of links: 'hard links' and 'soft (symbolic) links.'

A "hard link" creates a reference to an existing file that accesses the same exact data on the disk. For instance, I have a file named homework1.txt and I create a hard link called hw1.txt that references homework1.txt. Now, I edit hw1.txt. The changes I make to hw1.txt appear in homework1.txt. This also works the other way around. Changes to homework1.txt will affect hw1.txt. This is because both references (filenames) point to the same data in the computer. Therefore, I can see or change the same data either way I choose to access it. If I were to delete one of these files (even if I delete the original file and leave the hard link file I made), the data would remain intact through the other file. Data is only destroyed if I remove all references to it (ie. remove all links to it). You can keep track of how many links a file has through the 'ls -l' command (See: commonly asked questions).

A "soft (symbolic) link" can link to a file or a directory. This is commonly used to create a pseudo-directory that connects to another point in the file system. For instance, let us say that I wanted a link directly to the files located in the cse120 homework directory. In my home directory, I would create a link called cse120 that links to the cse120 homework directory. I could then navigate through that link using 'cd' as if it were a normal directory. If I decide to remove that link, it will have no effect on the files it points to.

Use the Following Command Very Carefully:

NameMeaningPurpose
ln LiNk Creates hard and soft file links.
Syntax
ln <existing_filename> <hardlink_filename>
ln -s <existing_filename> <symlink_filename>
Notes
  • There is actually a 'link' command which does the same thing but does no error checking, so most users are not allowed to use it.
  • Similarly, there is an 'unlink' command, but its use is not recommended.
  • Links can be removed using the 'rm' and 'rmdir' commands.


Designed by D. Kaminsky
Edited by Diana Palsetia
© University of Pennsylvania, 2008