Creating and Using Symlinks

Often we want some files to be in two places at once. Perhaps you are working a website that has its files in you home folder and then you want them to be displayed in /var/www/. Or maybe the software that your working with requests specific files in one folder but another program wants the in their current one. Despite the difficulty that I have in describing why you would need such an ability, There is know denying that eventually you are going to need to know how to use symlinks.

At the most basic level a symlink (AKA the Symbolic Link or sometimes referred to as a soft link) is a pointer that points to the actually location of the files without the program knowing that the files are not actually there. It also allows for a there to be differences in group and ownership if the content of a file, but more of that another time.

The Below code must be run in a directory which you have read and write privileges eg. /home/{your user name}

Lets first create a file using the touch (creates the file without opening it first) command and add some content using a directed cat (OH! so many commands) statement.

touch hello.one
cat This content is from hello.one > hello.one

next we will create a symlink to hello.two. This command follows a fairly simple structure of ln -s /path/to/file/ /path/to/fake/file

ln -s hello.one hello.two

Now lets see what is in hello.two. We can use cat again to view the contents (this time cat prints the contents of the file to the screen as the directional arrow is missing, rather than to the file).

cat hello.two

You should see the line that we added to hello.one earlier. eg. This content is from hello.one
Now that we have created the symlink. Lets remove it and the the files. This is as easy as deleting the file hello.two. while where at it we may as well clean up completely and remove hello.one as well.

rm -f hello.two
rm -f hello.one

IMPORTANT If you remove the file hello.one (or rename it) which is used by hello.two, then hello.two becomes useless but will still remain visible. This has got me once or twice when I didn’t know why I couldn’t access a file.

Symbolic links are also useful in changing the location of your directories. This works exactly the same as before. Lets say for example, you have a Code Igniter application, and you want to put the application folder in you home directory to use. the following command does just that.

ln -s /var/www/codeigniter/system/application mywebsite

Conclusion

Symbolic Links are very useful and when used correctly and greatly improve the accessibility of files and directories which makes all of our lives so much… easier.

Advertisements
Previous Post
Leave a comment

7 Comments

  1. Hi I just dropped by and wanted to say you to have a Merry Christmas. Let all your wishes make come true for you and your family and lets hope the next year be prosperous for all us.Merry Christmas

    Reply
  2. some genuinely interesting details you have written.

    Reply
  3. I your writing style genuinely enjoying this site.

    Reply
  4. As the saying goes, within the professional’s mind there aren’t many choices, but for someone possessing a beginner’s mind, everything is wide open.

    Reply
  5. Nice and easy! This is the way tutorials should be written.

    Reply
    • I’m Glad you liked it. I Hope you come back some time as I have many many more short and simple tutorials, blogs and articles swelling in my mind just waiting to be written.

      I’ve added you blog to the blog roll because you where the first to comment. 🙂

      Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: