How to Create, Update and Remove Soft link in Linux and UNIX - Example (original) (raw)

Symbolic links, Symlink or Soft link in Unix are a very important concept to understand and use in various UNIX operating systems e.g. Linux, Solaris, or IBM AIX. Symlinks give 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, the command to create a soft link and update soft link i.e. ln -s is also a must for anyone working in the UNIX machine. Whenever I do scripting or write any UNIX script I always write for symlinks rather than pointing to the absolute path of directories in UNIX.

It gives you the 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 their server running on Linux, since speed is a major concern in online stock or futures trading where orders have to hit the market within microseconds Linux server is an ideal choice for electronic and fixes trading systems.

And because your server is on Linux/UNIX you have to be expert of Linux 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 a 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.

How to Create, Update and Remove Soft link in Linux and UNIX

Create soft link hard link, update soft link and UNIX and LinuxThough 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 the example of UNIX symlinks I have seen during my projects of involving UNIX soft links:

  1. In our project, our Java process picks the latest version of the 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 rollbacks very easy which in turn increases stability and predictability of our Java application.

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

  2. 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 the permission of directory by using chmod command in Unix, permission on the soft link will also be updated.

In this section, we will see some differences between soft links and hard links 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.

  1. The first difference between a soft link and a 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. The second major difference between UNIX soft link and the 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 the soft link comes as the 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 between 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 the 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

Here we will see how to create a 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 a different version of a particular application and we will learn how to create a 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 the 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

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 a 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 is similar to removing any file; you need to use the “rm” UNIX command to remove any symlink. This will only remove the 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

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 a new package.

  2. Use pwd in a combination of UNIX soft link to find out the 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 the picture which tells you the 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 the 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.

Thanks for reading this article so far. If you like this article then please share with your friends and colleagues. If you have any questions or feedback then please drop a note.