create symbolic links in linux

How to Create Symbolic links in Linux

Symbolic Links or soft links are references to files & directories in Linux. They are basically shortcuts from one location to another in Linux file system. In this article we will look at how to create symbolic links in Linux, update symbolic link, create soft link to directory, and even remove soft link.


How to Create Symbolic links in Linux

You need to use ln command to create symbolic links in Linux. Here is its syntax.

ln -s [-f] [source] [destination]

Please note, ln creates hard link, that is, an actual copy of the file/folder, by default. So you need to use -s option to create symbolic links. -f option tells ln command to overwrite a symbolic link if it exists. Source is the file/directory to which you want to link. Destination is where symbolic link exists. If you don’t mention anything, then ln command will create symbolic link in your present directory.

Here is an example.

ln -s file.txt link.txt

In the above command, link.txt is the soft link that points to file.txt. Now when you run ls -l command in this folder, you will see the following output

$ ls -l
lrwxrwxrwx 1 ubuntu ubuntu 12 cen 21 14:40 link.txt->file.txt

In the above output the first character ‘l’ indicates that it is a link, not a file or folder. The last column of output, shows where this link points to, mentioned after ‘->’.


Create Symbolic Link to Folder

If you want to create symbolic links to folder or directory /etc/data, then here is an example for it.

ln -s /etc/data /home/data

In the above command /home/data points to /etc/data.


Update Symbolic Links

If a symbolic link of a given name already exists at a location, then you will get an error when you try to create/update the link with the above commands. In such cases, you need to use -f option overwrite/update the existing symbolic links. Here is an example.

ln -sf /etc/usr/data /home/data

Now the soft link /home/data will point to new location.


Remove or Delete Soft Links

You can easily remove or delete symbolic links using rm or unlink command as shown below.

$ rm link.txt
$ unlink /home/data
$ rm /home/project


Soft Links vs Hard Links

Here are the key differences between soft link and hard link.

Soft Link

Soft link is also known as symbolic link or symlink. It is basically a reference or link to a file and does not contain any content of the file. It is like a shortcut or hyperlink.

  • If symlink is deleted original will still remain unaffected
  • If original file is deleted symlink will stop working
  • Symlinks can refer to files on same of different file systems
  • It is commonly used to easily refer to file/directory with long paths


Hard Link

When a file is created in Linux it is assigned an inode that points to the actual file location on disk. Filename basically refers to this inode. Hard links is nothing but another filename for the same inode. So it is like creating a copy of the file.

  • If the original file is deleted, its hard link continues working
  • If the original file is moved, its hard link is not affected.
  • Hard links only work on the same file system. You cannot create hard links for files on another system.
  • When all the hard links pointing to inode are deleted, then the file’s inode and data are deleted for good.

In this article, we have learnt how to create symbolic links, and how to update & remove them.

Also read:

Ubuntu Change Terminal Font Color & Size
How to Remove Sticky Bit in Linux
How to Truncate File in Linux
How to Do Reverse DNS Lookup
How to SSH Using Pem file in Ubuntu

Leave a Reply

Your email address will not be published. Required fields are marked *