Symbolic links , Symlink or Soft link in Unix are very important concept to understand and use in various UNIX operating systems e.g. Linux , Solaris or IBM AIX. Symlinks gives you so much power and flexibility that you can maintain things quite easily.I personally feel that along with find, grep and other UNIX commands, command to create soft link and update soft link i.e. ln -s is also must for any one working in UNIX machine. 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 or soft link 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 Linux, 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. In this UNIX fundamental tutorial we will see How to create soft link in UNIX, How to update soft link and Difference between Soft link and Hard link in Unix and Linux. By the way this UNIX command tutorial is in continuation of my earlier article top networking commands in Unix and CVS command examples , if you haven’t read already you may find some useful information based on my experience in Unix and Linux commands.
Symbolic link or symlink in UNIX or Linux
Though this UNIX command tutorial is 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, by using tar archives, 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 directory , to which they are pointing out. which means if you change permission of directory by using chmod command in Unix, permission on soft link will also be updated.
Difference between Soft Link and Hard Link in UNIXIn this section we will see some 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 vs Hard link in UNIX
1) First difference between soft link and hard link is that Unix Soft links are pointers to programs, files, or directories located elsewhere (just like Windows shortcuts) , while Unix Hard links are pointers to programs and files, but NOT directories.
2) Second major difference between UNIX soft link and hard link is that 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. On the other hand, If the original program or file is renamed, moved, or deleted, the hard link is NOT broken
3) One not so important difference on soft link vs hard link is that, If you type ls -F you can see which files are UNIX soft links because they end with @
4) Another difference between soft link vs hard link is how you create them, 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. On the other side to create a UNIX hard link called myhardlink.txt that points to a file called myfile.txt, use this: ln myfile.txt myhardlink.txt
5) One more significant difference on soft link and hard link on UNIX or Linux is that, 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". While, 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
Creating a symbolic link or symlink in UNIX
Here we will see how to create soft link and hard link in UNIX, also known as symbolic link or symlink in 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 and 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 it is a link 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 or soft link 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
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.
Other Unix command tutorials from Javarevisited: