Sunday, July 25, 2021

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

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 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 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.



Symbolic link or symlink in UNIX or Linux

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.

2) All our UNIX 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 and environment 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.

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



Difference between Soft Link and Hard Link in UNIX

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.

Soft link vs Hard links in UNIX

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



Creating a symbolic link or symlink in UNIX

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




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 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 or symlink in UNIX

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



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 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.

14 comments :

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.

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...

Ahhh! thanks! ln -nfs works so wonderfully!

Jack said...

for updating soft link in Unix or Linux
ln -nfs is the way to go, forget about deleting old link and then recreating new link, just waste of time but that's what I was doing from long time.

Anonymous said...

One small typo:
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.

Instead of using "current" in the example, you used "latest"

Victor said...

What is UNIX Command to find Soft link or hard link ? I want to find all soft link and broken links in my server, do you know Which Linux or UNIX command I can use to find all symbolic links ? It's ok if they find both Soft link and hard link , I am more interested on broken links i.e. links which are there but files or directory to which they are pointing is deleted or removed.

Anonymous said...

I guess you left out the most important thing related to the difference between sym links and hard links. Hard links have the same inode number, because they are different pointer to the same space occupied on the disk, while soft links have different inodes

tang07059 said...

Agree with above comments regarding soft/hardlink. When you delete source file with a hardlink, the file is still accessible from hardlink, that's why hardlink doesn't break. Hardlink and original filename are almost equivalent just like multiple points point to same memory in C.

BHS said...

Hi Javin, this was a great tutorial. However, just had a small recommendation. Going forward, it would be great if you can describe what each of your options stand for while using the command. For example, while updating the symlinks using the command " ln -nsf 1.3 latest", it would be create if you would have just given a short description of what n,s,f stand for. Like n--> treat destination as a normal file, s--> creates a soft link, f--> removes the existing destination files(from symlink).

This way it is easier for newcomers like me to know how it is actually doing it rather than just mugging up the command and not knowing what each of the options are doing and why we need those options. We would need to surf the net to understand these options then.

Other than that, keep up the exceptional work. I am active subscriber of your blog and its the first thing I use to refresh or understand a concept. Thanks a lot.

Anonymous said...

Hi Javin,

I am very much impressed with this website. I appreciate you for your effort.
I want to create a symbolic link in Java6 environment. Could you please give me an idea, how we can create Symbolic links in Java6 environment.

Thanks in advance.

I think Java7 having java.nio.File packages to support this.

Anonymous said...

Hi Javin,

Thank you so much for nice article.How to display hardlinks in unix.I created hardlink with command ln file2 hardlink for file2.But when I do ls -ltr it is showing hardlink as file so it is difficult to differentiate between hardlink and source file

Post a Comment