how to create a symbolic link (hard or soft) in linux
Creating symbolic links or pointers to a file or folder is an important feature of posix based operating systems such as Linux and Unix. Links gives you the ability to create or duplicate "files" in the system with different names with out creating separate copies of the file. This makes it easier to update and modify a single copy where by all the links or shortcuts to the file are also updated.
There are essentially two different types of links that you can create in Linux: Soft links and Hard links. You should be aware of the differences between the soft links and the hard links, and probably already know which type of link you want to create.
The command to create symbolic links from the command line is ln. The same command can be used to create both the soft link as well as the hard links. There are several options available in the command line when creating links. we are take a look at few of the popular ones
-f or –force: This option will force the creation of the link even if there is an existing link or file at the destination by the same name.
-n or –no-dereference: This option will treat the destination file, if one already exists as a file. That means it will not follow the symbolic link even it is a link pointing to a directory.
-i or –interactive: Prompt for user confirmation before removing the destination file.
-v or –verbose: additional verbose information by printing the file names of the source and destination files.
All of the above commands can be used while creating either a symbolic or soft link as well as a hard link.
Creating a Hard link
By default, the ln command creates a hard link to the file. The following command will create a symbolic link to browser.css from the chromium folder whereby the chromium browser use that custom css.
ln /opt/conf/browser.css ~/.config/chromium/Default/User StyleSheets/Custom.css
Creating a Soft link or Symbolic Link
In order to create a symbolic link, rather than a hard link as described above, you will need to specify the –symbolic or -s option in the command line.
ln -s /opt/conf/browser.css ~/.config/chromium/Default/User StyleSheets/Custom.css
Using cp command to create links
Yet another option to create a symbolic link is the cp command. The cp command supports a command line option -s or –symbolic-link that will create a symbolic link to the file rather than copying it. This option is equivalent to creating a soft link using the ln command.
This does have some additional restrictions in that it requires absolute paths most of the time, unless the destination files are also in the same directory as the source.
cp -sv /root/temp/photos /root/current/photos
Another useful feature of the cp command is that it can recursively copy folders. If you use the –symbolic-link option along with the –recursive option to copy directories, it will create the new folder and then create symbolic links for each and every file and sub-folders to the original files inside the source folder. This can be quite handy if you want to replicate a complete directory structure but just as symbolic links.
cp -rsv /root/current/photos1/ /root/past/photos2/
The above command will create a symbolic link for each file inside the folder photos2/ that points back to the source file in the folder photos1/. Creating symbolic links can often be a good alternative to having to maintain different copies of the same files for use by different applications. A good example is the ability to share custom css file between various browsers on your system.