Symbolic Links

Let's talk about another type of link between files, the symbolic link.

There is one other type of link that is really useful, and it is called a symbolic link or sometimes a soft link. Hard links are somewhat limited as you can’t create one to a directory, for fear that you will create a cycle in the directory tree. You can’t hard link to files in other disk partitions, etc., because inode numbers are only unique within a particular file system, not across file systems. Thus, a new type of link called the symbolic link was created“A Fast File System for UNIX” by Marshall K. McKusick, William N. Joy, Sam J. Leffler, Robert S. Fabry. ACM TOCS, 2:3, August 1984. We’ll talk about the Fast File System (FFS) explicitly later on. Here, we refer to it because of all the other random fun things it introduced, like long file names and symbolic links. Sometimes, when you are building a system to improve one thing, you improve a lot of other things along the way..

Creating a symbolic link

To create such a link, you can use the same program ln, but with the -s flag. Here is an example:

Press + to interact
prompt> echo hello > file
prompt> ln -s file file2
prompt> cat file2
hello

Try it out yourself in the terminal below. You can run all the commands in this lesson in this terminal.

Terminal 1
Terminal
Loading...

As you can see, creating a soft link looks much the same, and the original file can now be accessed through the file name file as well as the symbolic link name file2.

However, beyond this surface similarity, symbolic links are actually quite different from hard links.

Differences between symbolic links and hard links

The first difference is that a symbolic link is actually a file itself, of a different type. We’ve already talked about regular files and directories; symbolic links are a third type the file system knows about. A stat on the symlink reveals all:

Press + to interact
prompt> stat file
... regular file ...
prompt> stat file2
... symbolic link ...

Running ls also reveals this fact. If you look closely at the first character of the long-form of the output from ls, you can see that the first character in the left-most column is a - for regular files, a d for directories, and an l for soft links. You can also see the size of the symbolic link (4 bytes in this case) and what the link points to (the file named file).

Press + to interact
prompt> ls -al
drwxr-x--- 2 remzi remzi 29 May 3 19:10 ./
drwxr-x--- 27 remzi remzi 4096 May 3 15:14 ../
-rw-r----- 1 remzi remzi 6 May 3 19:10 file
lrwxrwxrwx 1 remzi remzi 4 May 3 19:10 file2 -> file

The reason that file2 is 4 bytes is because the way a symbolic link is formed is by holding the pathname of the linked-to file as the data of the link file. Because we’ve linked to a file named file, our link file file2 is small (4 bytes). If we link to a longer pathname, our link file would be bigger:

Press + to interact
prompt> echo hello > alongerfilename
prompt> ln -s alongerfilename file3
prompt> ls -al alongerfilename file3
-rw-r----- 1 remzi remzi 6 May 3 19:17 alongerfilename
lrwxrwxrwx 1 remzi remzi 15 May 3 19:17 file3 ->
alongerfilename

Finally, because of the way symbolic links are created, they leave the possibility for what is known as a dangling reference:

Press + to interact
prompt> echo hello > file
prompt> ln -s file file2
prompt> cat file2
hello
prompt> rm file
prompt> cat file2
cat: file2: No such file or directory

As you can see in this example, quite unlike hard links, removing the original file named file causes the link to point to a pathname that no longer exists.

Get hands-on with 1400+ tech skills courses.