Symbol files in linux

10+ practical examples to create symbolic link in Linux

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:

ln [OPTION] TARGET LINK_NAME

TARGET refers to the file or directory for which you wish to create the link
LINK_NAME is the name of the link

It is recommended to use absolute path while creating a symbolic link or else you may end up with a broken link as a link must point to the path of the original file. You may also use relative path but cautiously

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:

# ln -s /tmp/orig_file.txt /root/orig_link

This will create a soft link /root/orig_link pointing to /tmp/orig_file.txt

Читайте также:  Reboot on linux cli

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.

# ls -l /tmp/orig_file.txt -rw-r--r-- 1 root root 0 Aug 2 18:32 /tmp/orig_file.txt

So to verify a link we must long list the symlink which we created

# ls -l /root/orig_link lrwxrwxrwx 1 root root 18 Aug 2 18:32 /root/orig_link -> /tmp/orig_file.txt

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:

# ln -s /tmp/orig_file.txt /root/

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

# ls -l /root/orig_file.txt lrwxrwxrwx 1 root root 18 Aug 2 18:39 /root/orig_file.txt -> /tmp/orig_file.txt

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

# tree /tmp /tmp ├── dir1 │ └── dir2 │ └── dir3 │ └── dir4

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

# ln -s /tmp/dir1/dir2/src_file /tmp/dir1/dir2/dir3/dir4/src_link # ls -l /tmp/dir1/dir2/dir3/dir4/src_link lrwxrwxrwx 1 root root 23 Aug 2 18:46 /tmp/dir1/dir2/dir3/dir4/src_link -> /tmp/dir1/dir2/src_file

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

# ln -s ../../../dir2/src_file src_link
# ls -l total 0 lrwxrwxrwx 1 root root 22 Aug 2 18:48 src_link -> ../../../dir2/src_file

A valid soft link would always be visible in CYAN colour (light blue), if you see your soft link in RED colour then it means that your soft link is in broken state

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

# ls -l /root/dir2 lrwxrwxrwx 1 root root 15 Aug 2 22:22 /root/dir2 -> /tmp/dir1/dir2/

So if I navigate inside /root/dir2

# cd /root/dir2 [root@server dir2]# pwd /root/dir2

I can still see the content of /tmp/dir1/dir2

[root@server dir2]# ls -l total 4 drwxr-xr-x 3 root root 4096 Aug 2 22:07 dir3 -rw-r--r-- 2 root root 0 Aug 2 18:45 src_file

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.

Читайте также:  Kaspersky linux agent удалить

create symbolic links (soft link)

create symbolic links (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.

# find /tmp/ -type l /tmp/dir1/dir2/dir3/dir4/src_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:

A hard link can be of a great security risk because a user can hard link configuration files and then easily exploit the system. The only way to defend your system is to ensure that system data and user data are in separate file systems because hard links can only be created when the source and the destination reside on the same volume.

In this example I have created a dummy file and place some content inside the file

# echo "Hello, This is a test file" >> /tmp/source_file

Next I will create a hard link of this file under /root/hard_link_file

# ln /tmp/source_file /root/hard_link_file

There is no output so this means the execution was successful. Next verify the hard link file

# ls -l /root/hard_link_file -rw-r--r-- 2 root root 27 Aug 2 21:45 /root/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

# echo "new content" >> /tmp/source_file

Now verify the content of hard_link_file

# cat hard_link_file Hello, This is a test file new content

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

# ls -l /root/source_file -rw-r--r-- 3 root root 39 Aug 2 22:00 /root/source_file

As expected, a hard link is created with the same name as source file

Читайте также:  32 bit wine on 64 bit linux

Now let us compare the stat of both source and hard link file

create symbolic links linux (hard link)

create symbolic links in Linux

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

# find /root/ -samefile /tmp/source_file /root/source_file /root/hard_link_file

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.

# ls -l /tmp/source_file -rw-r--r-- 1 root root 39 Aug 2 22:00 /tmp/source_file

Now let’s create a hard link of this file

Re-verify the link count of /tmp/source_file

# ls -l /tmp/source_file -rw-r--r-- 2 root root 39 Aug 2 22:00 /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

# find /tmp/ /root/ -type f -links +1 /tmp/dir1/dir2/dir3/hard_link_file /tmp/dir1/dir2/src_file /tmp/source_file /root/source_file

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.

# unlink /root/dir_link # unlink /root/source_file

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.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Источник

Оцените статью
Adblock
detector