Adam SMASH!

Symbolic links in linux

by Adam on Nov.19, 2009, under Linux, Ubuntu 9.04

Occasionally you may want to not have to type out the full path to a file or directory in your linux system or a network share. That is where links come in. Links are files that redirect to another location.

For instance, I have a 500gb external that I store music and backups etc. Well, I have a directory on there that I use alot, the FILM directory. I keep all the AVIs I’ve created from my DVDs for watching on my Media Center PC. The path is not super long, but once you understand symbolic links in Linux, you don’t have to type more than you want to.

The path to my film folder is /media/My\ Book/Video/Film but I want to be able to copy files to it easily from my home directory. So I will make a link to the directory in my home folder.

From my home folder I need to enter the following command:

ln -s /media/My\ Book/Video/Film FILM

ln is the command to create a link, -s will tell it to make a symbolic link. Symbolic links may point to any file or directory irrespective of the volumes on which the source and destination reside. Hard links cannot point to directories or other volumes. The next part is the path we want to link to. the last part, FILM, is the name of the link itself. This could be anything, but I made it film so it describes where I am linking to. But you could name it anything at all.

Now that I have my symbolic link in my home directory, as long as I am in my home directory, I can substitute FILM for the full path anytime I need to access it. For instance:

ls -la FILM

is now the same as

ls -la /media/My\ Book/Video/Film

And you can copy files to the FILM symbolic link and linux will redirect them to the directory linked to.

Also instead of just directories, you can symlink files. For instance, if you wanted to be able to be able to read your system’s auth.log file without having to type out the whole path to /var/log/auth.log, you could make a symlink in your home directory like so:

ln -s /var/log/auth.log authlog

This will make a symlink in the current directory called authlog pointed to /var/log/auth.log. Now all you have to do is type

cat authlog

in the directory containing the symlink et voila, you’re reading the auth.log file.

There are many other uses for symbolic links in linux, these are just a couple.


Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...