You’ve probably used the shortcut feature on your desktop at some point. Maybe you’ve got a button on your desktop that links to your “Documents” folder. It’s true that shortcuts make it much easier to navigate through your system; you don’t need to locate a particular folder where it is stored in order to access it.
The Linux command line offers a similar feature called symbolic links. Symbolic links allow you to create shortcuts to other files and folders on your system.
In this guide, we’re going to discuss what symbolic links are, why they are used and how you can create one using the Linux command line.
What Are Symbolic Links?
A symbolic link is kind of like a desktop shortcut. Symbolic links create connections between files and folders on your computer. When you interact with a symbolic link, they will reference the item at the other end.
What happens when you click on a shortcut on your desktop? You are taken to the folder to which that shortcut is pointing, right? The same happens with a symbolic link in Linux. Any time you run a symbolic link, it will run the file to which it is linked.
When you make a change to a file which is linked through symbolic links, every file will change. The original file acts as a central copy and every time you change it, every link will change as well; this is because a link is just a file that points to an existing file.
Symbolic links go by a number of names. You may hear them called symlinks, soft links, shell links, aliases or shortcuts.
The symbolic link is useful in helping you organize your file and folder structures. It also makes it easier to navigate different files on the operating system.
How to Create a Symbolic Link
Symbolic links are created using the
Let’s say we want to create a shortcut to the “linux_tutorial.txt” file in our “Documents” folder. This shortcut should appear in our “Desktop” folder. This command creates a symbolic link to our source file:
ln -s /Users/James/Documents/linux_tutorial.txt /Users/James/Desktop/linux_tutorial.txt
-s tells the
ln command line utility that we want to create a symbolic file link.
The first file name we have specified is the target file to which we want to link. The second folder name we have specified is where we want that file to appear. Let’s go to our desktop folder and check its contents. We can do so by using these commands:
cd Desktop ls -l
Our commands returned:
total 8 -rw-r--r--@ 1 James staff 2317 Jun 22 10:24 To-dos.md lrwxr-xr-x 1 James staff 41 Jun 22 10:41 linux_tutorial.txt -> /Users/James/Documents/linux_tutorial.txt
This shows us that a symbolic link has been created for our “linux_tutorial.txt” file. The “->” symbol indicates that there is a link between these two files.
You can also link directories in the same way. The following command allows us to create a link to the “Documents” folder on the desktop:
ln -s /Users/James/Documents /Users/James/Desktop/Documents
Symbolic Links in Action
Now that we’ve set up our symbolic links, we are ready to see how they work in action.
Our “linux_tutorial.txt” file contains the following text:
This is a tutorial!
Let’s change it to contain the text: “This is version two of the tutorial!” Once we save these changes, they will be reflected in the link we created to our file. If we check the contents of our symbolic link, you can see they have changed:
This command returned: This is version two of the tutorial!
As you can see, the shortcut we created to our “linux_tutorial.txt” file has changed. This is because a symbolic link is a pointer to the file; it’s not a copy of a file.
What Are Hard Links?
There are two types of links: soft links and hard links. Hard links are similar to symbolic or “soft” links.
Both hard links and symbolic links allow you to reference a file. The difference is that hard links link directly to the inode where a particular file or directory is stored on the disk.
Hard links are not just pointers to another file or folder. When you create a hard link, a copy of the original is created. This means that if you change the original file, the hard link will not be updated.
Most of the time you’ll use soft links on the Linux command line.
Symbolic links help you keep your files and folders organized. Linking is useful because it allows you to create a reference to another file and folder, thereby making it more accessible. When the underlying file changes, so does the link. A symbolic link is simply a pointer to another file.
It is important to know that when you delete a symbolic link or move it, all your symbolic links will become broken. Remember, symbolic links are not copies of a file; they are references.
You can learn more about creating symbolic links by running “man ln” in your terminal. This will open up the man page for the command where you’ll find more options for creating a symlink.