What is linux symbolic link

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.

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.

Let’s look at how you can create file and folder links in Linux:

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.

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

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.

Источник

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.

Читайте также:  Microsoft windows boot tool

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.

Источник

I nodes are associated with precisely one directory entry at a time. However, with hard links, it is possible to associate multiple directory entries with a single inode. Each file on Linux or Unix comes with an inode containing metadata about the file. To create a hard link use the ln command as follows:
ls /path/to/source /path/to/link
ln old_dir new_link_dir
ls [options] /path/to/source /path/to/link
## create a hard link ##
ln /home/vivek/my-awesome-long-file.txt /home/vivek/file
## create a symbolic link ##
ln -s /etc/resolv.conf /tmp/test
ls -l /tmp/test /home/vivek/file


Above commands create a link to my-awesome-long-file.txt and /etc/resolv.conf as /home/vivek/file and /tmp/test respectively.

Symbolic links refer to:

A symbolic path indicating the abstract location of another file. It is like a shortcut in Microsoft Windows operating system.

Hard links refer to:

The specific location of physical data. It an essentially a label or name assigned to a file.

  • Hard links cannot link directories.
  • Cannot cross file system boundaries.

Soft or symbolic links are just like hard links. It allows to associate multiple filenames with a single file. However, symbolic links allows:

  • To create links between directories.
  • Can cross file system boundaries.

These links behave differently when the source of the link is moved or removed.

  • Symbolic links are not updated.
  • Hard links always refer to the source, even if moved or removed.

You can create symbolic link with ln command:
ln -s source_file my_link
ln -s /path/to/file1.txt /path/to/file2.txt
ls -s /etc/hosts /tmp/file
ls -ali /tmp/file /etc/hosts
Above command will create a symbolic link to /etc/hosts as /tmp/file.

Let us create a directory called foo using mkdir command $ mkdir foo
$ cd foo
Copy /etc/resolv.conf file using the cp command, enter:
$ cp /etc/resolv.conf .
View inode number, enter:
$ ls -ali
Sample outputs:

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Now create soft link to resolv.conf, enter:
$ ln -s resolv.conf alink.conf
$ ls -ali
Sample outputs:

The reference count of the directory has not changed (total 152). Our symbolic (soft) link is stored in a different inode than the text file (1048602). The information stored in resolv.conf is accessible through the alink.conf file. If we delete the text file resolv.conf, alink.conf becomes a broken link and our data is lost:
$ rm resolv.conf
$ ls -ali
If alink.conf was a hard link, our data would still be accessible through alink.conf. Also, if you delete the soft link itself, the data would still be there. Read man page of ln for more information.

Conclusion

To create a symbolic link in Linux and Unix, at the shell prompt, enter:
ln -s source new_soft_link
ls -ali source new_soft_link
And to create a hard link in Linux and Unix, at the shell prompt, enter:
ln source new_hard_link
ls -ali source new_hard_link

Category List of Unix and Linux commands
Documentation help • mandb • man • pinfo
Disk space analyzers df • duf • ncdu • pydf
File Management cat • cp • less • mkdir • more • tree
Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
Linux Desktop Apps Skype • Spotify • VLC 3
Modern utilities bat • exa
Network Utilities NetHogs • dig • host • ip • nmap
OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
Package Manager apk • apt
Processes Management bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
Searching ag • grep • whereis • which
Shell builtins compgen • echo • printf
Text processing cut • rev
User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w
WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

Comments on this entry are closed.

Thank u Vivek Gite Sir,
for posting very nice article about linux filesystem…

Hard links cannot “links” directories?

Yes. Here is output from my desktop:

Vivek, what kaos meant was: the second word “links” should be written without the “s” in that sentence. It should be: “Hard links cannot link directories”.

Hardlink or softlink is only for files and not for folders.
/etc is a folder name. Choose any file name under it
like
$ln /etc/test.log /tmp/test_file

@Robertson
That is not really correct. Hard links cannot link directories however soft links can

soft links are often used to link to directories, e.g.
cdrom -> /media/cdrom/
floppy -> /media/floppy/

Ya the links cannot be used for directories and also you should not change the default file permissions of symbolic links file by using chmod command lest they will grant the user all the rights to the file. The sys admins therefore disable access to the /etc directory because if you had access to the directory a simple symbolic link to /etc/passwd could get you all the access you want to the system.
see this link

thank u vivek very good infomation
and nice explain.

I have created the softlink…….but not working

please let us know the output and how are you trying to access the softlink?

if you open nautilus and try to type the path of the soft link, it will appear as a short-cut (like in windows).

Try to browse it in X windows.

when i right click on a file then how the os gets it’s properties file. i.e in os where those files are stored

I knew that hard links cannot be linked to folders,However unable to find reason behind this? I did lot of googling but still unable to get correct answer Can somebody give me reason for this in simple english?

Hard links to directories and not supported, because you would be able to create “loops”.
Imagine you would go into a folder “a” and create a hard link “b” that would link to the folder “a”. This would create and endless loop. Imagine you would tar the directory a now.
Tar would go into directory a and find a directory b in it. Then it will try to go into b, but because it is a hard link to a, it will be in folder a again. There, it would again see the folder “b” and when it does into this folder, because of the hard link, end up in a again. And this would never stop, because you created a circle.
In this example it’s easy to figure out. But the situation might be more complex. A folder a might include – somewhere deep inside – a link to a different folder b and inside b there might be a link to a folder c and somewhere within c there would be a link to folder a. Now you have a circle again.
It’s not that it’s not possible to prevent programs from hanging etc., but they hard links to directories are a problem and it was decided to not allow them to make things easier.
Though, Apple uses them in Mac OS X for Time Machine, but does not allow the user to create them.

how the soft links and hard links will be helpful?

hi,
i have one file and one link file with the same filename as resolv.conf
lrwxrwxrwx 1 vivek vivek 11 2008-12-09 20:24 alink.conf -> resolv.conf
and
-rwxr-xr-x 1 vivek vivek 129 2008-12-09 20:19 resolv.conf
How can i open the content of the file resolv.conf which has a link file i.e. 1st one.

@Chaitanya
If you try to open either file with an editor or any thing for that matter they both will open resolv.conf.
resolv.conf is your real file and alink.conf is a symbolic link pointing to resolv.conf.
Though I’m sure you probably figured this out by now.

Softlinks and hardlink can be used for various things (organizing files is one of them).

Let me explain what you might do with links with the following example:

Let’s assume you have pictures in “

/Pictures” and music in “

/Music”, there is a folder for every artist you like and within that folder there is a folder for every album, which contains the songs of the artist; so you might have a folder “

/Music/Josh Woodward” and in that folder are the albums of Josh Woodward (that gives you “

/Music/Josh Woodward/Ashes” etc.).

You also have some Pictures of Josh Woodward in

You like to have all your pictures in

/Pictures and all your music in

/Music, but you don’t like that those folders are not close together. The pictures and the music of one artist are in completely different folders and you would also like to have all the music and all the pictures of an artist in one folder.

So you might want to create the folder “

/Artists” and in that folder you create a folder for every artist and within that you have a folder “Music” and a folder “Pictures” that contains the music and pictures of that artist. In this example you would have the folders

/Artist/Josh Woodward/Music and

/Artist/Josh Woodward/Pictures.
Now you have want you wanted and all the files of one artist are packed together in one folder.

The first idea would be to copy all the files into the new folders, but that leads to three main problems:
1. every file is saved twice on the disk, so you need twice the disk space, because every song will be in

/Artists//Music and so on.
2. if you would change the tags of a Picture in

/Pictures/… the same Picture in

/Artists would still have the old tags, you if you change a Picture, you need to copy it to the other locations to update the version there.
3. every new file needs to be saved to two places, which adds more work.

Problem two and three would be solved by creating scripts, but synchronizing the content could take some time. And problem one would still remain.

And now think you might want to go even further and for every artist you want to organize your albums in several ways: in “

/Music/by-artist” you have all your music organized by artists; in “

/Music/by-year” you have all albums organized by year and in

/Music/by-title” you just have all the albums without any further organization. To maintain such a structure can only be done with scripts and every time you add a new song somewhere, you need to run a synchronization script.

THIS is where links come into play.

/Artist/” folder, you do not create a folder “Music” and a folder “Pictures” and copy all the files; you just create a symbolic link “Music” to the folder “

/Music/” and a link “Pictures” to the folder “

Now, you can access the music and pictures through “

/Artists/” folder. But the files only need to be stored once on the hard disk AND if you put a new album in “

/Music//” it is immediately accessible in “

/Artists//Music”. And if you change the tags of a Picture, it does not matter if you go to “

/Pictures/…”, the changes will be accessible in both places.

You can’t create hard links in this case, because we are dealing with directories. But you can use hard links for the same thing. Hard links are just an additional name for the same file. So you in this example, you could create the folder “

/Artists///” and then in that folder create hard links for all the audio files.
In this case, adding new albums or new files to an album will not be visible in the other place until you create hard links for all new files as well. Hard links do have some advantages, however, because sometimes soft links will create problems in applications whereas hard links are “real files”. And the most important thing: If you create 100 symlinks to a single file and you delete that single file, all symlinks will stop working, because the symlinks are just links (like on the internet, remember?) and if you remove a site from the internet, all the links to it will stop working.

With hard links that will not happen; but hard links only work in the same file system.

I hope this helped a bit.

Nice explanation i need little more explanation about hard link with examples..

Thanks in advance.
Innova

Hi,
sorry for taking so long.

Hard links are essentially just additional names for the same file. A file in linux can have more than one name and be in more than one directory at the same time.
This might be a weird concept at first, because most people do not really use it, but it has some advantages (and disadvantages, depending on what you want to achieve).

Let’s do this as an exercise. Create a file named “test.txt” in your homefolder, open it with a text editor and add some text to it.
Now open a terminal and go to your home directory (should not be necessary, because a new terminal should start at your homedirectory, but still:

Now we can create a hard link and a soft link to this file and name them test_hard.txt and text_soft.txt, respectively:

ln test.txt test_hard.txt
ln -s text.txt test_soft.txt

If you look at your home folder now, you will see 3 files: test.txt, test_hard.txt and test_soft.txt.
If you open any one of them with a text editor, you should always get the same content.

Now rename test.txt to test_new.txt.

If you try to open test_hard.txt and test_soft.txt now, you will notice that test_hard.txt works just fine, but test_soft.txt does not. The reason why the soft link does not work anymore is because a softlink is basically a file that only contains the name of another file. When you created the soft link, you created a soft link to a file named “test.txt”.
If you now open the test_soft.txt, linux will know that it is soft link and then try to load the file the soft link is pointing to, which is test.txt. But test.txt is not there anymore, because you renamed it, so the file can not be loaded.

test_hard.txt works differently. A hard link does not just remember the path and name of the file where it links to, a hard link points directly to the data on the disk (well, the inode, to be exact, but that’s not important here), so the two files test_new.txt and test_hard.txt are pointing to the exact same data on the disk.
test_new.txt and test_hard.txt are now merely two different names for the exact SAME file.
You can rename each file without affecting the other (because they are both pointing to the data) and can also delete either one and the other one will still work.
That’s because Linux will remember how many names a file has. When you created the hard link, Linux noticed that there are now two names pointing to the file on the disk. When you delete either one, Linux knows that the other one is still being used and will therefore not delete the data on the disk, but only the filename.

And it does not matter which one you delete. You can delete test_new.txt and test_hard.txt will work just fine. Linux will only delete the data on the disk once there is no filename pointing to it anymore.

You can test that it is the same file, by editing it. No matter which one you edit, you will notice that the other does reflect the changes, because it’s just ONE file and test_new.txt and test_hard.txt are just two names for the same file. You can also move one of them to a different folder and it will still work.

My advice would be to play around with soft and hard links a little bit. Create a simple text file, created soft and hard links to it in different folders and see what happens.
Just one note: You can only create a hard link to a file on the same filesystem/partition.

I hope this could help you a little. If not, please feel free to ask more questions. I will try to answer them more quickly.

thanks a lot tordeu, putting efforts to explain the concepts.. now i m understand soft & hard links working manner.. currently i m working with linux BMR. so i need to analyze the different file system, file types behavior etc.. can u guide me what are things we need to concentrate linux while we take backup and recovered..

thanks in advance
innova

Thanks alot guys, the post and all the conversation was informative..;)

Hello,
Is it possible to create a link to another server?
For example ln -s /www/htdocs/link root@192.168.1.41:/www/htdocs/ ?

That command should be:
ln -s root@192.168.1.41:/www/htdocs /www/htdocs/link

For as far as I know, that won’t work.

But you can mount the remote directory instead (e.g. with sshfs). For example:
sshfs root@192.168.1.41:/www/htdocs/ /www/htdocs/dir/

The remote server needs to have SSH setup for that to work.

Thanks for your answer, SOLVED
You are right.
This is not possible from symbolic link, i had to install sshfs and then is possible
If some one is need this goto:
http://www.linuxnix.com/2011/03/mount-directory-locally-linux-sshfs.html

Another possibility (besides the already mentioned sshfs) would be nfs, which I would say is the better option if you want to be have access to the files on the server constantly.
Also, after mounting the server on your computer (no matter which method you use), you can then create symbolic links to those mounted files.

If your server has a folder foo and inside this folder is a file bar.jpg then you might mount the foo folder to /media/server. After it is mounted, the file bar.jpg will be available under /media/server/bar.jpg.
Now its possible to create a link to that file with
ln -s /media/server/bar.jpg bar

The reason you can’t directly create a soft link is because softlinks just store the path of the file you want to point to, so the file needs to be accessible locally somehow. It’s not possible to use IP addresses to access different systems.
You need to use something else (like sshfs, nfs, ftpfs, smb etc.) to first mount the files of the server locally. Then the files of the server have a local path through which they can be accessed (usually through /media/… or /mnt/…) and now that they have a local path, you can create a soft link to them.

This is very close to what I want to do. I have a NAS server running zfs. I access the zfs filesystem at it’s root with an NFS mount on my linux workstation as well with CIFS mounts on other users Windows boxes.

When I built the server I knew it would approach 80% capacity within 2-3 years. My original plan was to swap existing 2TB drives for 4TB ones, but I read in forums that resilvering even a 500MB disk could take 1-2 days, leaving one with a degraded file system during that time.

So, now I’m looking at putting another disk farm on the existing server via an eSATA connection and a port multiplier, then creating another zpool. I’d really like to keep the same directory structure (namewise) on the new file system as on the existing filesystem and make the fact that the users are accessing different zfs file systems transparent.

Would it be as simple as creating a soft link on the server (BSD) between the original file system and the new one?

Ex: on BSD, with the original file system at /mnt/nas/data, put the new file system at /mnt/nas/data1 then at root or at /mnt on tthenhe BSD server create a symbolic link: ln -s /mnt/nas/data1 /mnt/nas/data and point the NFS and CIFS mount points at /mnt/nas/data? Below the /mnt/nas/data would be directory one, two, three, four… and some of these would have further subdirectories before getting to files to be accessed.

So if I change the contents of a file I have a hard link for, what happens next ? The link works or not ?

I mean, does it work just like it did, with the changes visible just normally.

What is the performance difference between hard and soft ?

While creating soft link to hard drive, is it necessary to use -F option, like

System is implemented with CI framework.
But symbolik link is not working satisfactory.

I want to point from abc.com to dps.com’s order controller.
Actual code is on dps.com domain contoller order.
Then I want to point that controller as follows from abc.com

But when I open this url in browser then I am getting error page not found [404 error]
And When I open it as follows it’s working :

I do not understand whts going on.
Please help me.
In advance Thanks

I want to create symbolic link .

I want to point dps.com/order
order is controller file from abc.com

so that I can access abc.com/order/[param]

then I tried to create sym link for this but not working.
please give me solution.

your extensive use of quoting does not clarify

this article is the opposite of useful: jumbled

Hi,
1st I created both hardlink (hlink) and softlink (slink) to a single file (a.dat).
As I know, slink has its own inode, and this inode will point to the inode of a.dat. But hlink does not have its own inode. when we create a hardlink it will directly point to the inode of the a.dat.

So my qs here is when we are removing Original File (a.dat), because of the existence of a hardlink (hlink), the inode of a.dat still be there in memory.But why we can not access this content by the slink, though slink’s inode points to the inode which is still exist in the memory, whereas hlink can access though it is pointing to the same inode.

Please help me understand this and correct me if i am wrong..

Источник

Читайте также:  Windows server 2012 r2 отличие от windows server 2012 standard
Оцените статью