How to get a list of Symbolic Links on Linux

On Linux, within the directory structure, the symbolic links are critical. Using a file browser it is easy to know this because they are identified, but in a terminal how to do it? Well, today you will learn How to get a list of Symbolic Links on Linux.

Introduction—What is a symbolic link on Linux?

The term symbolic link or Symlink refers to a type of file that points to another file. This target file can be local or refer to a remote file. As a rule, the symbolic link inherits the permissions of the referenced file.

One of its main advantages is that you can use symbolic links to help you search for or access certain files that are not so quickly accessed.

If you come from Windows, you will know the so-called Shortcuts because symbolic links work similarly to these.

Symbolic links vs. hard links

Symbolic links are often called soft links. The main characteristic of these is that any modifications or changes you make will be reflected in the original. However, if you delete the link, only the link will disappear and not the authentic file.

However, hard links behave in the same way as the original file. This means that if you delete the hard link, you will also delete the target file. Another feature of hard links is that you cannot make a hard link in a different file system.

The use of one or the other depends on your needs and does not make one better than the other.

Create symbolic links on Linux

On Linux, there is a command called ln which refers to Link and is used to create links between files. This command has an -s option that tells the command to only make a symbolic link.

Concretely, to create a symbolic link on Linux it is necessary to follow this syntax

 ln -s [source file] [symbolic name]

For example,

 ln -s /home/imaginelinux/Documents/file.txt file

So, when you access file it is as if you were doing it directly in /home/imaginelinux/Documents/file.txt.

In general, that is the way to make symbolic links on Linux. However, keep in mind that the system has its own symbolic links and so there are many of them.

How to get a list of Symbolic Links on Linux

If you have made many symbolic links, you will notice that sometimes it can be difficult to get them all. However, by using a few commands, the process is easy.

Finding all the Symbolic Links of the System

The most common thing to do is to find out which symbolic links are in the entire system. To achieve this, the most sensible thing to do is to use the find command, which will do a search for you and your chosen criteria.

It is as simple as opening a terminal and executing

sudo find / -type l

You will have a wide screen output like this

Get a list of Symbolic Links on Linux
Get a list of Symbolic Links on Linux

You will then have a complete listing of these links.

Get the symbolic links of a specific directory

If you wish, you can get the symbolic links in any directory. To achieve this, just run.

sudo find [directory-path] -type l

It can be any directory. For example,

sudo find /home/user -type l

Or

sudo find /home/user/Documents/ -type l

Get the symbolic links of the current directory

In case you want to get the symbolic links of the current working directory, you can use the . as argument.

sudo find . -type -l

This way, only those in the updated directory at the prompt will be counted.

You can also limit the depth of the search. If you only would like to limit it to one level, then run.

sudo find . -maxdepth 1 -type l

This way, subdirectories will not be included.

Counting symbolic links on Linux

By combining the above commands with the wc command, you can find out the number of symbolic links you have in the directory or in the whole system.

You can do this as follows

sudo find /home/user -type l | wc -l

As output on the screen, you will have the number of symbolic links

3988

Conclusion

The symbolic links are important elements of the system that allow to have more orderly access to files. Knowing how many there are and where they are is a good way to keep them under control.

Please help us to grow and share this post with your friends.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top