Home > Articles

  • Print
  • + Share This

Creating Symbolic Links

Symbolic links are convenient shortcuts used to link existing files or directories to files or directories with more convenient locations or names. By default, a number of symbolic links are created when Linux is installed:

  • Various shells listed under the /bin directory, such as bsh, csh, and sh, are actually symbolic links to other shells, such as bash or tcsh.

  • The computer's modem device can be a link to /dev/modem instead of /dev/cua1 or /dev/ttyS1.

  • The ex and vi editors are links to the vim text editor.

Linking Files with the ln Command

There are times when you find that it is convenient to have more than one path to the same file. Say that you want to easily be able to execute an application from anywhere, without typing the entire path to the file. You might create a link from /usr/local/bin to the file's executable.

There are two kinds of links in the Linux world: hard links and soft links. They have similar effects but are two very different beasts.

Soft Links

A soft link is a very small file that you create in a directory. The sole content of this file is the path to the file you are linking it to. So if you create a soft link from /home/michael/foo to /usr/local/bin/foo, you could execute the file, /usr/local/bin/foo, and it would behave as if you were executing /home/michael/foo. However, the file in /usr/local/bin/ would be nothing but a set of instructions pointing the shell to the file in /home/michael. Think of it as call forwarding for your shell! Also, keep in mind that if you delete the original file, your soft link will point at nothing and will be totally useless. (Just like if you forward all your calls to your cellular phone but then turn the cell phone off, your calls will have nowhere to go and the call forwarding will be useless.)

Soft links will work across networked file systems, mounted devices, other file systems, and directories.

To create a soft link, use the ln command with the -s (symbolic link) command-line option, with the original filename followed by the name of the desired link:

$ ln -s graphicwithalongname.xcf graphic.xcf 
$ ls -l  
-rw-rw-r--   1 michael    users   16821 Feb 10 15:22  
lrwxrwxrwx   1 michael    users       4 Feb 10 15:22  
graphic.xcf-> graphicwithalongname.xcf 

Notice that if you type ls -l, the directory listing for the soft link reflects the file to which it points.

Hard Links

Hard links work a bit differently than soft links. Let's return to the call forwarding analogy. If a soft link is like call forwarding for a phone number, a hard link would be like having multiple phone numbers that point to the same phone.

Every file on your computer is written to a physical spot on your hard drive. That spot is called an inode and inodes each have a number. When you type ls for a directory listing, you are really looking at an index that tells the file system which inode corresponds to each file. If you type ls -i, you will see a directory listing with the inode numbers:

$ ls -i 
1091611 saved-messages  1091701 sent-mail  1091610  

When you create a hard link, you are just creating another directory listing to point to the same inode. You can create as many hard links on the system to a single file as you would like without taking up any more disk space. If you delete the original file, the second remains because you have not deleted the inode to which it points. The only way you can delete any file on the file system is by deleting all the hard links to that file. If you make changes to the original file, all the links will reflect that change (because they are all pointing to the same inode).

Hard links do not work across networked file systems, partitions, mounted devices, or any other file system. They also do not work for directories. You can only create a hard link to a file on the same disk and partition as the original.

Use the ln command (without the -s option) to create a hard link:

$ ln graphicwithalongname.xcf newgraphic.xcf 
$ ls -l file* 
-rw-rw-r--   2 michael    users    16821 Feb 10 15:22  
lrwxrwxrwx   1 michael    users        4 Feb 10 15:22  
graphic.xcf -> graphicwithalongname.xcf 
-rw-rw-r--   2 michael    users    16821 Feb 10 15:22  
$ ls -i  
 45146 graphicwithalongname.xcf    45146 newgraphic.xcf 

Notice that both the files in the example above have the same inode number.

Linking Directories with the ln Command

The ln -s command can also be used to create links to often-used directories. (Note you cannot hard link directories.)

$ ln -s /var/http/html/  /home/amy/website

Now, instead of having to type a long pathname when moving files, you can use the following instead:

$ mv *jpg website/
  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.