[email protected]Office hours: Mon – Fri 9:00AM – 5:00PMLinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedMarch 16, 2023

How to understand symbolic link and their use.

How To Understand Symbolic Link And Their Use.

A symlink or a Symbolic Link is simply enough a shortcut to another file. It is a file that points to another file.

They may sound a little complex, but they’re really not, you can think of symlinks as shortcuts. To create a symbolic link you use the following command:

directory that symbolic link point to.

ln -s target_path link_pat

 

Symlinks can take two forms:

Soft links are similar to shortcuts, and can point to another file or directory in any file system.

Hard links are also shortcuts for files and folders, but a hard link cannot be created for a folder or file in a different file system. These can be created in the following way on Linux and Mac operating systems.

 

Soft links are similar to shortcuts, and can point to another file or directory in any file system.

Hard links are also shortcuts for files and folders, but a hard link cannot be created for a folder or file in a different file system.

Let’s look at the steps involved in creating and removing a symlink. We’ll also see what broken links are, and how to delete them.

 

The syntax for creating a symlink is:

ln -s <path to the file/folder to be linked> <the path of the link to be created>

ln is the link command. The -s flag specifies that the link should be soft. -s can also be entered as -symbolic.

By default, ln command creates hard links. The next argument is path to the file (or folder) that you want to link. (That is, the file or folder you want to create a shortcut for.)

And the last argument is the path to link itself (the shortcut).

 

How to Create a Symlink for a File – Example Command

ln -s /home/james/transactions.txt trans.txt

After running this command, you will be able to access the /home/james/transactions.txt with trans.txt. Any modification to trans.txt will also be reflected in the original file.

Note that this command above would create the link file trans.txt in your current directory. You can as well create a linked file in a folder link this:

ln -s /home/james/transactions.txt my-stuffs/trans.txt

There must be a directory already called “my-stuffs” in your current directory – if not the command will throw an error.

 

How to Create a Symlink for a Folder – Example Command

Similar to above, we’d use:

ln -s /home/james james

This would create a symlinked folder called ‘james’ which would contain the contents of /home/james. Any changes to this linked folder will also affect the original folder.

 

Before you’d want to remove a symlink, you may want to confirm that a file or folder is a symlink, so that you do not tamper with your files.

One way to do this is:

ls -l <path-to-assumed-symlink>

Running this command on your terminal will display the properties of the file. In the result, if the first character is a small letter L (‘l’), it means the file/folder is a symlink.

You’d also see an arrow (->) at the end indicating the file/folder the simlink is pointing to.

There are two methods to remove a symlink:

 

The syntax is:

unlink <path-to-symlink>

This deletes the symlink if the process is successful.

Even if the symlink is in the form of a folder, do not append ‘/’, because Linux will assume it’s a directory and unlink can’t delete directories.

 

As we’ve seen, a symlink is just another file or folder pointing to an original file or folder. To remove that relationship, you can remove the linked file.

Hence, the syntax is:

rm <path-to-symlink>

For example:

rm trans.txt
rm james

Note that trying to do rm james/ would result an error, because Linux will assume ‘james/’ is a directory, which would require other options like r and f. But that’s not what we want. A symlink may be a folder, but we are only concerned with the name.

The main benefit of rm over unlink is that you can remove multiple symlinks at once, like you can with files.

 

Broken links occur when the file or folder that a symlink points to changes path or is deleted.

For example, if ‘transactions.txt’ moves from /home/james to /home/james/personal, the ‘trans.txt’ link becomes broken. Every attempt to access to the file will result in a ‘No such file or directory’ error. This is because the link has no contents of its own.

When you discover broken links, you can easily delete the file. An easy way to find broken symlinks is:

find /home/james -xtype l

This will list all broken symlinks in the james directory – from files to directories to sub-directories.

Passing the -delete option will delete them like so:

find /home/james -xtype l -delete

 

Wrapping up

Symbolic link are an interesting feature of Linux and UNIX systems.

You can create easily accessible symlinks to refer to a file or folder that would otherwise not be convenient to access. With some practice, you will understand how these work on an intuitive level, and they will make you much more efficient at managing file systems.

Keep reading
How to Implement Network Segmentation and Resource Isolation in Linux.
February 6, 2024

How to Implement Network Segmentation and Resource Isolation in Linux.

How to Implement Network Segmentation and Resource Isolation in Linux. Securing and managing networks in a Linux environment involves advanced practices like network segmentation and resource isolation. This guide explores these concepts, providing detailed instructions and examples for…

Read article
How to open or close ports in Linux.
February 6, 2024

How to open or close ports in Linux.

How to open or close ports in Linux. Effectively managing ports on your Linux system is crucial for both security and service accessibility. This comprehensive guide will provide a detailed walkthrough on opening and closing ports using the robust tools iptables and ufw. Understanding these…

Read article
How to use ypdomainname command.
January 23, 2024

How to use ypdomainname command.

How to use ypdomainname command. In the realm of Linux networking, the ypdomainname command plays a crucial role in the context of the Network Information Service (NIS). NIS, formerly known as Yellow Pages, is a distributed database service that facilitates the sharing of network configuration and…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, colocation, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Office hours: Mon – Fri 9:00AM – 5:00PM