- 10+ practical examples to create symbolic link in Linux
- Types of symbolic links
- Comparison soft link vs hard link
- How to create symbolic links
- Create soft link
- Example-1: using different link name
- Example-2: using same link name as source file
- Example-3: use relative path for soft link
- Example-4: Create soft link for directory
- Check the stats of soft link
- Find all the soft links
- Create Hard Link
- Example-1: using different link name
- Example-2: use same link name as the source file
- Check the stats of hard link file
- How do we identify hard links?
- Example-1: Using find with samefile
- Example-2: Using find to check link counts
- How to remove symbolic links
- Conclusion
- Related Posts
- How to Create Symbolic Links in Linux
- What are Linux Symbolic Links
- How to Create Symbolic Links in Linux?
- Create Symbolik Link in Linux for Files
- Create Symbolic Link in Linux for Folders
- How to Change or Remove Symbolic Link in Linux?
- Wrapping up
10+ practical examples to create symbolic link in Linux
Table of Contents
In this tutorial we will learn everything about symbolic links in Linux. I will try to be as descriptive as possible so even a newbie can easily understand the concept of symbolic links. A symbolic link can also be referred as symlink so don’t get confused if you see me using these words through out this article.
Types of symbolic links
There are two different types of symlinks in Linux, namely soft links or hard links. Before we go ahead, let us understand the basic difference between these types:
Comparison soft link vs hard link
Soft Link | Hard Link |
---|---|
The inode number of the actual file and the soft link file are different. | The inode number of the actual file and the hard link file are the same. |
A soft link can be created across different filesystems. | A hard link can only be created in the same filesystem. |
A soft link can link to both regular files and directories. | A hard link doesn’t link to directories. |
Soft links are not updated if the actual file is deleted. It keeps pointing to a nonexistent file. | Hard links are always updated if the actual file is moved or deleted. |
So I hope now you have some idea about symbolic links in Linux. I will give some examples for both types of symbolic links to help you understand better.
How to create symbolic links
With Linux and Unix we have ln utility which is used to create symbolic links. The basic syntax to create links would be:
TARGET refers to the file or directory for which you wish to create the link
LINK_NAME is the name of the link
Create soft link
Let’s use some examples to understand the concept of soft links and how we can create one.
Example-1: using different link name
In this example, I have a file /tmp/orig_file.txt . I wish to create a symlink for this file under /root with a different name orig_link . So our command would look like:
This will create a soft link /root/orig_link pointing to /tmp/orig_file.txt
Verify the symlink. Now when we long list the orig_file.txt , it has no information of any links which are created for the file.
So to verify a link we must long list the symlink which we created
Here the symbolic link /root/orig_link is pointing to it’s original file location
Notice lrwxrwxrwx in the permission section of symlink, here «l» represents soft link
Example-2: using same link name as source file
In the earlier example we had explicitly given a name for our soft link. If we just provide the path instead of name then a soft link with the same name as source file will be created.
Let’s check this in our example:
Now here we have not given any name for our softlink but just the PATH i.e. /root so this has created a soft link soft_file.txt under /root
Example-3: use relative path for soft link
Now in the above example we were using absolute path for the soft link which is always recommended and easy. But may times the absolute path can be dynamic so in such case we have to use relative path for the softlink.
For example, I have created a directory structure inside /tmp
I have a original source file inside /tmp/dir1/dir2/src_file and I want my symlink to be inside /tmp/dir1/dir2/dir3/dir4
Now ideally I can give the absolute path and create soft link by using
But for the sake of this article I will use relative path assuming I am inside /tmp/dir1/dir2. The trick here is, always make sure you are inside the location where you want to create your soft link i.e. in this case our symlink must be inside /tmp/dir1/dir2/dir3/dir4 so we navigate to this path
and then create the symlink using relative path of src_file
now verify the symlink
Example-4: Create soft link for directory
We can also create soft links for directories. The existing command syntax can be used, just replace the file with directory for which you wish to create symlink. In this example we will use the same name of the link as the source directory i.e. dir2
Verify the soft link:
So if I navigate inside /root/dir2
I can still see the content of /tmp/dir1/dir2
Check the stats of soft link
Now since we are familiar with how to create soft links in Linux, let’s learn little more about the soft link file type. We can use stats to get the file details and compare both source file and soft link.
stat of source file
stat of soft link
As you can check in the images:
- Both source file and symlink has different inode numbers
- Both source file and symlink have different size. The symlink will be hardly few bytes.
Find all the soft links
We can use find command to locate all the soft links in the Linux server using -type as » l » which means soft links.
Create Hard Link
A hard link in generic terms is a legal occurrence of the same file on a file system in multiple directories. When created, a hard link serves simply as a reference to an existing file, and the link maintains the exact same attributes as its source file. Thus, if ownership, mode, or even the containing data is changed on a link, those exact same changes will be realised on the source file.
We can use the same ln utility to create hard link. We do not need any argument with ln , the syntax to create hard link would be:
Example-1: using different link name
In this example I have created a dummy file and place some content inside the file
Next I will create a hard link of this file under /root/hard_link_file
There is no output so this means the execution was successful. Next verify the hard link file
As you can see, unlike softlink the hard link file is not pointing to it’s source file and is acting as a normal file.
I will add some data to the source file
Now verify the content of hard_link_file
So the same data is automatically added into the hard link file.
Example-2: use same link name as the source file
In this example we will not define a name for our hard link file hence it will pick the same name as the source file
Now verify the hard link file
As expected, a hard link is created with the same name as source file
Check the stats of hard link file
Now let us compare the stat of both source and hard link file
stat of source file (hard link)
stat of hard link file (hard link)
As you can see both files are completely identical, even the inode numbers are same. Normally the inode numbers for every file which is created in Linux will be different but since here both files are linked the inode numbers are same.
How do we identify hard links?
We can use find command to locate all the hard links in Linux.
Example-1: Using find with samefile
With find we can use -samefile argument which will look out for all the files having same inode number
Example-2: Using find to check link counts
Now one interesting thing which you will notice with hard links. This is my source file where the value before «1 root root» is 1 which means the file has 1 link i.e. for the same file.
Now let’s create a hard link of this file
Re-verify the link count of /tmp/source_file
The link count has increased to «2» so every time we create a hard link of a file, the link count is increased.
So we can use this with find command to locate all the hard links
How to remove symbolic links
Since symbolic links are also like a regular file we can use rm command to remove symlinks but in Linux we have a separate utility i.e. unlink which is used to remove symbolic links
We do not need any argument with unlink , you can just execute it in the below format
For example we will delete the file and directory links we created earlier. The same command can be used for both soft and hard links.
Alternatively you can also use rm command to remove symbolic links as they are also a reular file
Conclusion
In this tutorial we learned all about symbolic links in Linux, how to create, remove, find, check different types of symbolic links. This should cover most part of symlinks but if you wish to learn more then you can always refer the man page of ln command which can give you some more insights on supported options and arguments.
Lastly I hope the steps from the article to create symbolic links on Linux was helpful. So, let me know your suggestions and feedback using the comment section.
Related Posts
Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud
If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.
For any other feedbacks or questions you can either use the comments section or contact me form.
Thank You for your support!!
Источник
How to Create Symbolic Links in Linux
Learning Linux symbolic commands is a great way of improving your potential in the Linux terminal. In this tutorial, we’ll cover a few commands to learn symbolic links in a quick and easy way. Before we begin, let’s overview what are symbolic links.
What are Linux Symbolic Links
Symbolic Links are not only helpful in creating shortcuts and file management in operating systems like Linux. They also serve as a way to create various locations for primary user folders, for instance, Documents, Pictures, Downloads, and much more!
Symbolic Links act like a string creating pathways for different files, folders, and directories in the computer system. They are capable of creating and storing multiple files in different places refer to one single file. Thus, increasing efficiency by locating all the specific documents in one command.
These links are stored in the mainframe, so even if the original file is deleted, you’ll have a backup for most of the important files. Symbolic links help create invalid link pathways to store pieces of information as per the requirement of the user.
Due to the user-friendly features in Linux, even Microsoft is following it to create Symbolic Links. Symbolic links, also known as Soft links or Symlinks, are not unique to Linux but they are just like a Search option in Windows where one can search a specific file or directory in a disk by executing various commands.
How to Create Symbolic Links in Linux?
Let’s look at how you can create file and folder links in Linux:
Create Symbolik Link in Linux for Files
Generally, to create links use we use the ln command and the -s option to specify Symbolic links. This is the easiest way to ensure a flexible approach that allows experimenting with the language as much as possible. There is nothing hard in creating Symbolic links in Linux – you just need to follow one simple step.
The ln command in Linux creates links between source files and directories.
- -s – the command for Symbolic Links.
- [target file] – name of the existing file for which you are creating the link
- [Symbolic filename] – name of the symbolic link.
Created links can be verified by directory listing using detailed list command:
However, if you do not specify the [Symbolic filename], the command will automatically create a new link in the existing directory.
Create Symbolic Link in Linux for Folders
Creating symbolic links for folders is not difficult either. The command used to create the folder symbolic link is:
For example, to link the /user/local/downloads/logo directory to /devisers folder, use the following command:
Once a Symbolic link is created and attached to the folder /devisers, it will lead to /user/local/downloads/logo. When the user changes directory – cd – to /devisers, the system will automatically change to the specific file and write it in the command directory.
Symbolic link options are called command line switches. Here are the most common ones and their descriptions:
Command Switch | Description |
–backup[=CONTROL] | backup each existing destination file |
-d, -F, –directory | superuser is allowed to attempt hard link |
-f, –force | existing destination file is removed |
-I, –interactive | prompt before removing destination files |
-L, –logical | deference targets that are symbolic links |
-n, –non-dereference | symbolic links to directory are treated as files |
-P, –physical | make hard links directly to symbolic links |
-r, –relative | create symbolic links relative to link location |
-s, –symbol | make symbolic links instead of hard links |
-S, –suffix=SUFFIX | override usual backup suffix |
-v, –verbose | print name of each linked file |
How to Change or Remove Symbolic Link in Linux?
You can remove existing links attached to files or directories by the unlink or rm command. This is how you can do it with the unlink command:
Removing symbolic link using the rm command is similar to the unlink command which is as under:
Wrapping up
Remember, if the source is no longer in the current location, then you should delete the symbolic files to avoid creating duplicates, which might slow down your work.
Linux is a wonderful platform for creating an interactive and dynamic application, where you can experiment and innovate. A strong foundation is critical. Learn the basic of the language thoroughly to use it to its full potential. We hope this tutorial helped you improve your skills with another useful tool!
Edward is an expert communicator with years of experience in IT as a writer, marketer, and Linux enthusiast. IT is a core pillar of his life, personal and professional. Edward’s goal is to encourage millions to achieve an impactful online presence. He also really loves dogs, guitars, and everything related to space.
Источник