How to create a symbolic link in Linux

Linux FAQ: How do I create a symbolic link in Linux?

Answer: To create a symbolic link in Linux, just use the Linux ln command, like this:

ln -s OriginalFile NewSymbolicFile

As you can see from my filenames, when using the Linux ln command, you specify the name of the file you're linking to first, and then supply the name of the link second.

After issuing the command shown above, when I now issue an "ls -l" command, I now see something like this, showing the symbolic link between the original file and the new "file":

lrwxr-xr-x   1 Al  staff      12 Jun  4 19:39 NewSymbolicLinkFile -> OriginalFile
-rw-r--r--   1 Al  staff   1,000 Jun  4 19:39 OriginalFile

As you can see in that listing, the NewSymbolicLinkFile is shown with an arrow pointing at the OriginalFile. That's one way you can tell this "file" is really a symbolic link. Another way to tell is by the lowercase letter 'L' in the first column of that listing:

lrwxr-xr-x

The 'l' in that first column also shows that this file is really a symbolic link. (A "-" in the first column indicates a normal file, and a "d" indicates a directory.)

Creating a symbolic link to a file in another directory

I just ran across the problem where I needed to create a symbolic link to another file so I could get my files backed up with the Dropbox online backup service.

The way Dropbox works is that your data needs to appear to be in a special Dropbox folder in your home directory. You can move all your data into that Dropbox folder if you like, or, you can do what I did, and create a symbolic link to your data.

In the example shown here, I created a symbolic link to a directory named "Music" that I keep on my Mac Desktop:

$ cd Dropbox

$ ln -s ~/Desktop/Music .

After creating this symbolic link to my Music folder, when I use the Linux ls command to list the files in my Dropbox folder, I now see a symbolic link to my Music folder.

lrwxr-xr-x  1 Al staff   23 Jun 4 19:33 Music -> /Users/Al/Desktop/Music

One thing to note about the Linux ln command I use in that example is that I didn't specify a name for the symbolic link, I just used this command:

$ ln -s ~/Desktop/Music .

By just using the decimal/period key at the end of that command, I told the Linux ln command to just use the same for the link as the name of the file or folder that I linked to.

That's one more important thing to note: You can use symbolic links in Linux to link to either a file or a folder, no problem.

Linux ln command - creating a hard link

Using the Linux ln command you can also create something named a "hard link". I don't use these too often, in part because they can get confusing. Therefore, I won't say much about hard links with the ln command, but I will share one example.

In this example, I have a file named Getting-Started.pdf, and I know that I want to create a hard link to this file. To do this, I again use the Linux ln command, but this time I leave off the "-s" argument, like this:

ln Getting-Started.pdf foo.pdf

Here I have create a "hard link" between the file named Getting-Started.pdf and a new file named foo.pdf. If you issue an "ls -l" command at this point, you'll see that there is a significant difference between a Linux symbolic link and a hard link:

-rw-r--r--  2 Al  staff  269894 May 21 17:00 Getting-Started.pdf
-rw-r--r--  2 Al  staff  269894 May 21 17:00 foo.pdf

As you can see, there is no arrow pointing from one file to the other, and no letter 'l' in the first column. The only giveaway that these files are actually the same file is a) their file size is the same, and b) the number "2" just after the permissions column indicates that there are two links to the file.

There are other ways to tell that these files are indeed the same, including the use of the md5sum command. But I don't want to talk about this ln command feature because I don't use it much any more. If someone else would like to add some comments about it below, you're welcome to do so.

Linux ln command and symbolic links - summary

I hope this brief tutorial on the ln command and symbolic links in Linux has been helpful. I've intentionally kept it brief, but if you have any questions, or would like to see any more symbolic link examples, just let me know what you'd like to see.