Make symbolic links linux

This detailed tutorial tells you what are symbolic links, how to create a symbolic links and other important things associated with symlinks.

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 change the content of the actual target file.

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

lrwxrwxrwx 1 abhishek abhishek 23 Jul 2 08:51 link_prog -> newdir/test_dir/prog.py

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 Linux Terminal

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

ln -s target_file link_name

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.

To know which real file the link actually points to, use the realpath command:

There are other ways to follow a soft link to its source file but realpath is the easiest.

Deleting the link won’t delete the source file it links to.

Читайте также:  Драйвера для рутокен linux

You can delete multiple symbolic links in one command as well:

There is also an unlink command. But unlike the impression its name gives, the unlink command is not specifically used for deleting links. It can remove files and folders and links, of course. However, it has certain limitations that make rm command a better choice even for deleting links.

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:

-rw-r--r-- 1 abhishek abhishek 163 Apr 13 15:07 newdir/test_dir/prog.py

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

ln -s newdir/test_dir/prog.py link_prog

Here are the attributes of the newly created link:

lrwxrwxrwx 1 abhishek abhishek 23 Jul 2 08:51 link_prog -> newdir/test_dir/prog.py

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.

touch link_prog ls -l newdir/test_dir/prog.py -rw-r--r-- 1 abhishek abhishek 163 Jul 2 10:04 newdir/test_dir/prog.py

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.

ln -s non_existant_dir link_dir less link_dir link_dir: No such file or directory

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.

lrwxrwxrwx 1 abhishek abhishek 23 Jul 2 08:51 link_prog -> newdir/test_dir/prog.py

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.

Читайте также:  Linux hdd serial number

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.

Источник

Dillion Megida

Dillion Megida

Symlink Tutorial in Linux – How to Create and Remove a Symbolic Link

A symlink (also called a symbolic link) is a type of file in Linux that points to another file or a folder on your computer. Symlinks are similar to shortcuts in Windows.

Some people call symlinks «soft links» – a type of link in Linux/UNIX systems – as opposed to «hard links.»

Soft links are similar to shortcuts, and can point to another file or directory in any file system.

Hard links are also shortcuts for files and folders, but a hard link cannot be created for a folder or file in a different file system.

Let’s look at the steps involved in creating and removing a symlink. We’ll also see what broken links are, and how to delete them.

The syntax for creating a symlink is:

ln is the link command. The -s flag specifies that the link should be soft. -s can also be entered as -symbolic .

By default, ln command creates hard links. The next argument is path to the file (or folder) that you want to link. (That is, the file or folder you want to create a shortcut for.)

And the last argument is the path to link itself (the shortcut).

ln -s /home/james/transactions.txt trans.txt 

After running this command, you will be able to access the /home/james/transactions.txt with trans.txt . Any modification to trans.txt will also be reflected in the original file.

Note that this command above would create the link file trans.txt in your current directory. You can as well create a linked file in a folder link this:

ln -s /home/james/transactions.txt my-stuffs/trans.txt 

There must be a directory already called «my-stuffs» in your current directory – if not the command will throw an error.

This would create a symlinked folder called ‘james’ which would contain the contents of /home/james . Any changes to this linked folder will also affect the original folder.

Before you’d want to remove a symlink, you may want to confirm that a file or folder is a symlink, so that you do not tamper with your files.

Читайте также:  Grub error unknown filesystem при установке linux

Running this command on your terminal will display the properties of the file. In the result, if the first character is a small letter L (‘l’), it means the file/folder is a symlink.

You’d also see an arrow (->) at the end indicating the file/folder the simlink is pointing to.

There are two methods to remove a symlink:

This deletes the symlink if the process is successful.

Even if the symlink is in the form of a folder, do not append ‘/’, because Linux will assume it’s a directory and unlink can’t delete directories.

As we’ve seen, a symlink is just another file or folder pointing to an original file or folder. To remove that relationship, you can remove the linked file.

Note that trying to do rm james/ would result an error, because Linux will assume ‘james/’ is a directory, which would require other options like r and f . But that’s not what we want. A symlink may be a folder, but we are only concerned with the name.

The main benefit of rm over unlink is that you can remove multiple symlinks at once, like you can with files.

Broken links occur when the file or folder that a symlink points to changes path or is deleted.

For example, if ‘transactions.txt’ moves from /home/james to /home/james/personal , the ‘trans.txt’ link becomes broken. Every attempt to access to the file will result in a ‘No such file or directory’ error. This is because the link has no contents of its own.

When you discover broken links, you can easily delete the file. An easy way to find broken symlinks is:

This will list all broken symlinks in the james directory – from files to directories to sub-directories.

Passing the -delete option will delete them like so:

find /home/james -xtype l -delete 

Wrapping up

Symbolic link are an interesting feature of Linux and UNIX systems.

You can create easily accessible symlinks to refer to a file or folder that would otherwise not be convenient to access. With some practice, you will understand how these work on an intuitive level, and they will make you much more efficient at managing file systems.

Dillion Megida

Dillion Megida

Developer Advocate and Content Creator passionate about sharing my knowledge on Tech. I simplify JavaScript / ReactJS / NodeJS / Frameworks / TypeScript / et al My YT channel: youtube.com/c/deeecode

If you read this far, tweet to the author to show them you care. Tweet a thanks

Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started

freeCodeCamp is a donor-supported tax-exempt 501(c)(3) charity organization (United States Federal Tax Identification Number: 82-0779546)

Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons — all freely available to the public. We also have thousands of freeCodeCamp study groups around the world.

Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.

Источник

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