How to Create Symbolic Links in Linux
Symbolic links or symlinks are one of the most important Linux features, especially for aspiring Linux sysadmins. As the name suggests, symbolic links allow users to point a file toward another file without mirroring its data, essentially providing you with multiple places to access the same file. So, in this guide, let’s look at how you can create symbolic links in Linux.
Symlinks are often referred to as soft links and it different from another type of link in Linux – hard link. The difference between hard and soft links is that a hard link to a file will be able to access the contents of the original file and will retain its contents if the original file were to be deleted. Whereas, a soft/symbolic link is like a shortcut in Windows, which points to the original file but becomes useless once the file it’s pointing to is deleted.
How to Create a Symbolic Link (Symlink) in Linux
Creating a symbolic link in Linux is simple. All you need to do isuse the “ln” commandalong with the “-s” identifier to tell your Linux system that you are trying to make a symlink. Here, “ln” is short for Link, and this command is used to create a Symbolic Link. It’s usually used in conjunction with an identifier followed by either two file names or paths. Here’s the syntax for the ln command in Linux:
Here’s how you can create a symlink in Linux using the ln command:
-
Launch the Terminal and ensure you’re in the same directory as the file you want to link to.
-
Type the following command in the Terminal. Make sure to use the “-s” identifier followed by the name of the file and the new name. Here “1” is the actual file and 1-1 is the name of the symlink file.
-
Similarly, you can also create a symbolic link for directories and link them to a particular folder using the ln command. In the below example, we mention the directory that we want to link first (/home/abubakar) and then mention the folder we want to link it to, which here is folder 1.
How to Overwrite Symbolic Links in Linux
If you want to overwrite a Symlink to replace it with a new revision of the file you’re linking to, you can easily do so using the“-f” force parameter. Linux doesn’t allow you to overwrite existing symlinks by passing only the same file names, rather you’ll need to use the -f parameter. Here’s the syntax to overwrite symlinks:
Now, let’s see how to overwrite symlinks in Linux using an example. Below, we are overwriting 1.txt with 2.txt using the ln command:
How to Remove Symbolic Links in Linux
Likeaccessing Linux files from Windowsand creating symlinks, removing them is pretty easy too. Here’s how to do it.
-
For a file, you could go ahead and remove the symlink to break the link. You can do this by usingLinux’s rm commandin the directory which holds the symlink.
-
For folders, again, you could delete the link using the“rm -rf” command.
-
Here’s how to use the unlink command to unlink a folder from its symlink.
How to Find and Remove Broken Symlinks in Linux
From the “Overwrite Symlinks example” if I delete the file “1.txt,” it will create a broken symlink “2.txt.”
Now, to find this broken symlink in Linux, use the following command. If you are in the same directory as the broken symlink, you don’t need to mention the path. Just use the command syntax below:
Finally, execute this command to get rid of the broken Symlink:
Beebom Staff
Bringing the latest in technology, gaming, and entertainment is our superhero team of staff writers. They have a keen eye for latest stories, happenings, and even memes for tech enthusiasts.
Add new comment
Name
Email ID
Δ
01
02
03
04
05