Symbolic link linux ��� ���

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.

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:

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.

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

Let’s use some examples to understand the concept of soft links and how we can create one.

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

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

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

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

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.

We can use find command to locate all the soft links in the Linux server using -type as » l » which means soft links.

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:

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.

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

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.

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

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

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.

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!!

Источник

A symbolic link, also known as a symlink or a soft link, is a special type of file that simply points to another file or directory just like shortcuts in Windows. Creating symbolic link is like creating alias to an actual file.

If you try to access the symbolic link, you actually access the target file to which the symlink points to. Changes performed on the content of the link file changes the content of the actual target file.

If you use the ls command with option -l, this is what a symbolic link looks like:

In most Linux distributions, the links are displayed in a different color than the rest of the entries so that you can distinguish the links from the regular files and directories.

Soft Link displayed in different color

Symbolic links offer a convenient way to organize and share files. They provide quick access to long and confusing directory paths. They are heavily used in linking libraries in Linux.

Now that you know a little about the symbolic links, let’s see how to create them.

To create a symbolic link to target file from link name, you can use the ln command with -s option like this:

The -s option is important here. It determines that the link is soft link. If you don’t use it, it will create a hard link. I’ll explain the difference between soft links and hard links in a different article.

Symbolic links could be confusing at times therefore you should keep note of a few things.

That’s the whole purpose of the links after all. You access the target file by accessing the link. You can make changes to the target file through the links. Let’s see with example.

I have a file prog.py in newdir/test_dir. It has the following attributes:

Now, I’ll create a soft link to this file in my present directory:

Here are the attributes of the newly created link:

Notice the l (it’s L, not one) at the beginning of the line? If you are familiar with the file permissions in Linux, you would know that the ‘l’ signifies link and thus it tells you that this file is actually a link. To refresh your memory, – means file, and d means directory.

Now if I use this link to change the content or the attributes, the same will be reflected in the target file. For example, I am using touch command on the soft link and you’ll notice that it changes the timestamp of the target file.

How would you know if the link points to file or a directory? You cannot know that until you follow the path and access the target file itself.

Yes, that’s totally possible. This is why you should be careful while creating soft links in Linux. The target file to which you are linking doesn’t need to exist. You won’t get any error or warning for creating link to a file/directory that does not exist.

You’ll get error only when you try to access the target file, either through the link or on its own. The ls command will still work though.

Did you notice the file permission on the symbolic link? The symlinks are always created with 777 permission (rwxrwxrwx). For regular file, this would mean that anyone can access the file. But that’s not the case for the links.

If the file permissions on the links were treated as it is, any user could create a symlink to a secure file and access it freely. That would be a major security issue. Thankfully, that doesn’t happen. Because the permission on the target files matter, not the permission on links.

You may use the chmod command to change the permission on the link but it will change the permission of the linked file, not the link itself.

You can make a symbolic link that points to another link and so on. This is called chained symbolic link. It’s better to avoid them as it creates more confusion.

Well, that’s it. I presume you have a better knowledge of the soft links now and you know how to create symbolic links in Linux. You may read about the symlinks command that can help you find broken symlinks in Linux and manage them easily.

If you have questions or suggestions, please leave a comment below.

Источник

Читайте также:  Zenmate vpn для windows крякнутый
Оцените статью