Thursday, April 21, 2011

How to create update or remove symbolic or soft link in Unix and Linux

symbolic link or symlink in unix linuxSymbolic links or symlinks are the heart of UNIX commands in my opinion. Symlinks gives you so much power and flexibility that you can maintain things quite easily. Whenever I do scripting or write any UNIX script I always write for symlinks rather than pointing to absolute path of directories in UNIX. It gives you flexibility of changing the symlink without making any change on your tried and tested scripts. I have worked on many different core java projects which run on Linux and UNIX machine and make extensive use of UNIX symbolic links or symlinks. All my project which are on finance domain and on electronic trading systems have there server running on Unix. since speed is major concern in online stock or futures trading where orders has to hit the market within micro seconds Linux server is ideal choice for electronic and fix trading systems and since your server is on UNIX you have to be expert of Unix command to work efficiently and these articles are my result of those effort to learn and share new UNIX commands.


Symbolic link or symlink in Unix Linux

This UNIX command tutorial is  in continuation of my earlier article Top 10 basic networking Commands in Unix  and Top 10 most useful CVS command in Unix  and 10 examples of using find command in UNIX  if you haven’t read already you may find some useful information based on my experience in Unix and Linux commands.

Though this UNIX command tutorial is dedicated to highlight Differences between soft link in UNIX and hard link in UNIX which is also a very popular UNIX command interview question, I am not able to resist myself by showing you the usage of soft link in UNIX, below are some of example of UNIX symlinks I have seen during my projects of involving UNIX soft links:

1) In our project our Java process picks latest version of package for executing, which is a UNIX soft link. So whenever we do a release we just need to update latest UNIX symlink which makes release seamless and rollback very easy which in tern increases stability and predictability of our Java application.

2) All our UNIX script takes the location as argument so they are agnostic about the absolute path of resources and these resources are provided them via UNIX soft links and environment variables. This feature of our scripts saves a lot of time whenever we need to do any migration which involves changing location of resources.

3) An important point about UNIX soft link is that they inherit the permission of the folder to which they are pointing out.


Difference between Soft Link and Hard Link in UNIX


symbolic link in unix, soft and hard link in unix linux
Now here are few points which highlight Differences between soft link and hard link in UNIX. These differences are by no means complete so please contribute if you know any other difference between UNIX soft link and hard link. You can also let us know about how you are using symlinks or UNIX soft links.

Soft link in UNIX

•           Unix Soft links are pointers to programs, files, or directories located elsewhere (just like Windows shortcuts)
•           If the original program, file, or directory is renamed, moved, or deleted, the soft link is broken and it will show in red color if you using ls -lrt --color option.
•           If you type ls -F you can see which files are UNIX soft links because they end with @
•           To create a soft link called "current" that points to a file or directory called "new_package", use this: ln -s new_package latest  to remember this command always remember that name of soft link comes as last argument.

*      UNIX soft link can point to a network mounted directory also. For creating unix soft link remember to use option "-s" with UNIX link command "ln".


Hard link in UNIX

•           Unix Hard links are pointers to programs and files, but NOT directories
•           If the original program or file is renamed, moved, or deleted, the hard link is NOT broken
•           Hard links in UNIX cannot span disk drives, so you CANNOT have a hard link on /dev/hdb that refers to a program or file on /dev/hda
•           To create a UNIX hard link called myhardlink.txt that points to a file called myfile.txt, use this: ln myfile.txt myhardlink.txt



Creating a symbolic link or symlink in UNIX


Here we will see how to create symbolic link or symlink in UNIX or Linux. For our symlink example we will use a folder called symlinks which will have some directories representing different version of a particular application and we will learn how to create symlink, remove symlink and update symlink or soft link in Unix Linux.

Here is the initial snapshot of our example symlink directory, currently there is no symlink in directory.

Javin@unix_machine ~/symlinks
$ ls -lrt
total 0
drwxr-xr-x+ 2 Javin None 0 Apr 22 12:14 1.2
drwxr-xr-x+ 2 Javin None 0 Apr 22 12:14 1.3

Now we will create a soft link in UNIX in symlink directory.

Javin@unix_machine ~/symlinks
$ ln -s 1.3 latest

This will create a soft link name “latest” which will point to directory “1.3”. Let’s see whether this soft link in UNIX created or not.  We can see that in last line a symlink is created. Notice lrwxrwxrwx  (first “l” denotes its symlink in unix)

Javin@unix_machine ~/symlinks
$ ls -lrt
total 1
drwxr-xr-x+ 2 Javin None 0 Apr 22 12:14 1.2
drwxr-xr-x+ 2 Javin None 0 Apr 22 12:14 1.3
lrwxrwxrwx  1 Javin None 3 Apr 22 12:17 latest -> 1.3

Updating a symbolic link or symlink in UNIX


We have seen how to create a symlink in UNIX now we will see how we can update that symlink without removing it.

Javin@unix_machine ~/symlinks
$ ln -nsf 1.2 latest

This will update the symlink latest to point to directory “1.2” instead of “1.3”. notice command line option “-nsf”. Now let’s see whether this symlink is updated or not.

Javin@unix_machine ~/symlinks
$ ls -lrt
total 1
drwxr-xr-x+ 2 Javin None 0 Apr 22 12:14 1.2
drwxr-xr-x+ 2 Javin None 0 Apr 22 12:18 1.3
lrwxrwxrwx  1 Javin None 3 Apr 22 12:19 latest -> 1.2

Now let’s create another link previous in this directory pointing to 1.2 and latest pointing to 1.3

Javin@unix_machine ~/symlinks
$ ln -nsf 1.3 latest

Javin@unix_machine ~/symlinks
$ ln -s 1.2 previous

Javin@unix_machine ~/symlinks
$ ls -lrt
total 2
drwxr-xr-x+ 2 Javin None 0 Apr 22 12:14 1.2
lrwxrwxrwx  1 Javin None 3 Apr 22 12:20 latest -> 1.3
drwxr-xr-x+ 2 Javin None 0 Apr 22 12:21 1.3
lrwxrwxrwx  1 Javin None 3 Apr 22 12:24 previous -> 1.2

If you have noticed here that when you run “ls –lrt” , total will display number of soft link in UNIX present in that directory , you can see on previous examples its changes from 0 to 1 and now its 2 because of two symlink present here.

Removing a symbolic link or symlink in UNIX


Removing a symbolic link is similar to removing any file; you need to use “rm” UNIX command to remove any symlink. This will only removes symlink and not delete the source directory.

Javin@unix_machine ~/symlinks
$ rm latest previous

Javin@unix_machine ~/symlinks
$ ls -lrt
total 0
drwxr-xr-x+ 2 Javin None 0 Apr 22 12:14 1.2
drwxr-xr-x+ 2 Javin None 0 Apr 22 12:18 1.3

UNIX Symbolic link or Symlink Tips

symbolic link or symlink in unix, soft and hard link in unix linux
Here are few more tips related to creating soft link in UNIX

1) Use ln -nfs to update the soft link. As I have said earlier we had a latest symlink which points to latest package and every time we do a release we need to update this Linux soft link, before knowing this option I used to first delete the old soft link and then creates a new soft link which is a two step process but this is the fasted way of doing it just execute "ln -nfs new_pakcage latest" and latest soft link will point to new package.

2) Use pwd in combination of UNIX soft link to find out actual path your soft link is pointing out. Many times we need to know where we are after navigating through various soft and hard link and here pwd comes in picture which tells you absolute path of your present working directory in UNIX.

3) To find out all UNIX soft link and hard link in any directory execute following command "ls -lrt | grep "^l" ". It’s an extension of my UNIX command improvisation to finding out all directories in any directory by using command "ls -lrt | grep "^d" ".

Javin@unix_machine ~/symlinks
$ ls -lrt | grep "^l"
lrwxrwxrwx  1 Javin None 3 Apr 22 12:20 latest -> 1.3
lrwxrwxrwx  1 Javin None 3 Apr 22 12:24 previous -> 1.2

here we are making use of fact that "ls" command in Unix displays "l" in front of every entry and then we do a UNIX "grep" for lines starts with "l" for links and "d" for directories.

Do you like Books?
If you love to read books here are few Java titles which is worth of money :
Please vote +1 or consider sharing if your like this article

12 comments:

Helen Neely said...

Lovely tips. I guess any self-respecting Linux/UNIX user should know at least half of the most used commands on the platform.

Nice blog BTW

arapidhs said...

Very comprehensive. Thank you.

Javin @ Electroinc Trading and FIX Protocol said...

@Helen Neely , Thanks you like this UNIX soft link tutorial. I agree these are most of these tips already known by users but this post is meant for starters and new comers and for those who already knows UNIX soft link it could be a refresher :)

Javin @ FIX Protocol and Electroinc Trading said...

@arapidhs, Thanks for your comment , good to hear that you like the tips.

priyanka said...

I am glad to read this as ! best tip is renaming the link.saves two steps.thanks :)

Anonymous said...

Hi Can these command related to soft link and hard link is also applicable for other flavor of unix like Solaris and AIX ? By the way I really like your tips on updating Soft link in one step. earlier I had to delete and recreate the link for just rename. Thanks

Anonymous said...

Thanks for the nice explanation.
It really helped me

Anonymous said...

The best tip I got on updating symbolic link in Unix is using "ln -nsf" to forcefully update the soft link without deleting it.

Anonymous said...

Too much words for so little content. You could say the same with half the words.

Anonymous said...

Thank you

Anonymous said...

Thanks a lot.

Anonymous said...

Ahhh! thanks! ln -nfs works so wonderfully!

Post a Comment

What is this blog about

This blog is about my experience in Java, Tibco Rendezvous and FIX protocol. FIX is a technology which is used to build equity trading system and heavily used in electronic trading , high frequency trading and Algorithmic trading, so on most of Investment bank job you need to know FIX Protocol on the other hand Tibco Rendezvous is used to implement messaging backbone on many big commercial and global banks. This blog help you to get clear any FIX protocol job interview. It also includes Java and Tibco Rendezvous article written for new user mainly my experience in my own word.