Find All Symbolic Links in Linux

Find All Symbolic Links in Linux

How do you find a soft link?

You can use the ls command. Some distributions show the links in a different color. The long listing is always reliable because it shows links with l.

lrwxrwxrwx 1 abhishek abhishek 14 Jan 31 18:07 my_link -> redirects.yaml

You can also use the tree command:

Find All Symbolic Links in Linux

This is okay if you have a couple of links in the current directory. But what if you want to see the links in a nested directory structure or the entire system?

In this tutorial, I will be showing you two ways to accomplish this mission:

  • Using the find command
  • Using the symlinks utility

So let’s start with the first one.

To find the symbolic links using the find command, you can use the following command syntax:

find Target_directory -type l

For example, here, I searched for available symbolic links inside the Links directory:

find Links/ -type l
Find All Symbolic Links in Linux

But by default, the find command will initiate the recursive search and if you want to limit the search to a certain depth, you will have to use the -maxdepth flag.

So let’s say I want to restrict the search to level 1 for the Links directory, I will be using the following:

find Links/ -maxdepth 1 -type l
Find All Symbolic Links in Linux

And if you want detailed output including the file permissions, user groups, etc. then you will have to pair the find command with -ls flag:

find Target_directory -type l -ls
Find All Symbolic Links in Linux

If you want a system-wide search, you can use / in the command.

This tool is what I used while pursuing my internship in networking.

But it does not come pre-installed though. You can install it using your distribution’s package manager. For Ubuntu/Debian, use:

sudo apt install symlinks

Once you are done with the installation, use the given command structure to look for available symbolic links:

symlinks -v target_directory
Find All Symbolic Links in Linux

Here, the -v option gives verbose output.

But by default, the symlinks utility won’t look into subdirectories. Enable recursive search with the -r option:

symlinks -vr target_directory
Find All Symbolic Links in Linux

The output has specific terms. Let me explain them.

  • relative indicates that links are relative to the current working directory in which the link resides.
  • other_fs means the link is indicating a different filesystem. In my case, it is indicated to the external drive.

Really, they might sound like a huge deal but we made sure to break the topic bit by bit.

Such as if you are a complete beginner, you can refer to the beginner’s guide to symbolic links:

How to Create Symbolic Links in Linux [Complete Guide]
This detailed tutorial tells you what are symbolic links, how to create a symbolic links and other important things associated with symlinks.
Find All Symbolic Links in Linux

And if you want to follow them to their origin, you can refer the following guide:

How to Follow Symbolic Links in Linux
You got a symbolic link and wondering about the actual source file? Here’s how to follow symlinks in Linux.
Find All Symbolic Links in Linux

I hope you will find this guide helpful. And if you have any queries or suggestions, be my guest in the comments section.

How to Delete Soft Links in Linux

How to Delete Soft Links in Linux

The symbolic link, also known as soft link or symlink, in Linux is a special type of file that works as a shortcut to another file.

You can create a soft link using the ln command. But what about deleting them?

There is no special command for removing symbolic links in Linux. You can use the rm command which is also used for deleting files and directories.

rm symbolic_link_name

You may also the unlink command here. Don’t go by its name. It’s not only for deleting links; it can also delete files.

unlink symbolic_link_name

Let’s see it in detail.

All you have to do is to give the name of the path of the link to the command:

rm name_or_path_of_link

Let’s see it with an example. Can you identify the soft link in the long list output of ls command?

abhishek@LHB:~/test$ ls -l
total 4708
-rw-rw-r-- 1 abhishek abhishek 4794657 Sep 27 20:36 export.json
-rw-rw-r-- 1 abhishek abhishek     311 Sep 22 12:19 line.txt
lrwxrwxrwx 1 abhishek abhishek      26 Oct 17 11:24 mylink -> ./Documents/sample-mark.md
-rw-rw-r-- 1 abhishek abhishek     106 Sep 27 20:39 redirects.json
-rw-r--r-- 1 abhishek abhishek   12817 Sep 22 12:28 sample.txt

The link is called mylink. You can identify it in the long listing as it starts with the character l (for link) and the name shows the file it points to.

Let me delete the link and verify it:

abhishek@LHB:~/test$ rm mylink 
abhishek@LHB:~/test$ ls -l
total 4708
-rw-rw-r-- 1 abhishek abhishek 4794657 Sep 27 20:36 export.json
-rw-rw-r-- 1 abhishek abhishek     311 Sep 22 12:19 line.txt
-rw-rw-r-- 1 abhishek abhishek     106 Sep 27 20:39 redirects.json
💡
Deleting a soft link does not delete the original file it links to.

You can verify that the original file still exists.

How to Delete Soft Links in Linux

You can remove multiple symlinks at once with the rm command:

rm symlink1 symlink2 symlink3

Another way to delete soft links is by using the unlink command. It may sound like this command is only for removing links, it can delete files as well.

To remove a link with unlink, use it like this.

unlink name_or_path_of_link

I’ll use the same example I used earlier.

How to Delete Soft Links in Linux

The unlink command is pretty limited. You can not delete multiple links at once with it.

You can create soft links to both files and directories. While you have to remove a directory with -r option, the link to a directory doesn’t need that.

Use it the same way you used to delete link to a file:

rm name_or_path_to_link_to_dir

Don’t use trailing slashes with the link else it will complain.

abhishek@LHB:~/test$ rm link_to_dir/
rm: cannot remove 'link_to_dir/': Is a directory

Here’s an example of deleting a soft link to directory.

How to Delete Soft Links in Linux
⚠️
Never force remove a link to a directory with rm -f because it will delete the contents of the directory.
How to Delete Soft Links in Linux
Force deleting the link to the directory deletes the contents of the actual directory

Unlike soft links, a hard link is almost indistinguishable from the original file. You can only notice it with the inode number.

Can you identify the hard link and the file in the output? Pay attention to the inode numbers.

abhishek@LHB:~/test$ ls -li
total 4716
 544057 -rw-rw-r-- 1 abhishek abhishek 4794657 Sep 27 20:36 export.json
 544884 -rw-rw-r-- 2 abhishek abhishek     311 Sep 22 12:19 hard_link
 544884 -rw-rw-r-- 2 abhishek abhishek     311 Sep 22 12:19 line.txt
1181365 drwxrwxr-x 2 abhishek abhishek    4096 Oct 17 12:33 my_dir
 546419 -rw-rw-r-- 1 abhishek abhishek     106 Sep 27 20:39 redirects.json

And deleting a hard link is the same as deleting a file.

rm path_or_name_of_hard_link

What about deleting the linked file?

I can’t think of a scenario where you would want to delete the original file while removing the soft link automatically.

Well, you can follow a symbolic link to get to the original file and use it to remove the file.

rm "$(readlink '/path/to/link')" /path/to/link 
💡
If you delete a file, keeping the soft link intact, the link becomes a broken or dangling link.

Conclusion

While the unlink command exists, I recommend using the rm command for deleting symbolic links. You are already familiar with it and use this command for removing files. Use it for links as well. It’s one less command to remember.

🐧LHB Linux Digest #22.11: Concept of Links, Podman, Vim and More

🐧LHB Linux Digest #22.11: Concept of Links, Podman, Vim and More

Do you like the LHB Linux Digest newsletter? If yes, would you like to provide a 100-200 characters testimonial along with your name, country and job title (student, home user, self hoster, retired teacher, sysadmin with x years of experience etc)? You can concisely share what you like about the newsletter by replying to this email.

I plan to showcase some testimonials on the newsletter page (in works). It’s your chance to be famous! Just kidding 😉

💬 Let’s see what you have in this month’s issue:

  • All about links in Linux
  • Selected new articles on Linux Handbook
  • And the usual newsletter elements like memes, deals and nifty tool to discover

How to Follow Symbolic Links

How to Follow Symbolic Links

A symbolic link (also known as soft link) is a kind of shortcut to another file. It’s heavily used in Linux for shared libraries.

But how do you know to which original file the link points to?

You can use the ls command for this purpose. Surprised? Don’t be. The long listing ls -l displays where a symbolic link points:

ls -l /path/to/file

For example, I’ve created a soft link named MyTorrents that targets another disk so my command will be:

ls -l /home/sagar/Symbolics/MyTorrents
How to Follow Symbolic Links
A symbolic link is indicating to its original file

However, this is not a foolproof way to follow the symbolic link to the original file because if it’s a multilayer link (a link pointing to another link that points to a file), the ls command won’t display the source file.

It’s a no-brainer that with enough skills, you do have multiple ways of accomplishing the same thing, especially if we consider Linux.

So I’ll be utilizing the following command line utilities to follow symbolic links:

  • readlink
  • realpath
  • stat
  • file

You can use the ln command to create links and practice while you follow this tutorial.

A specific utility that is just made to accomplish our goal. Yes, that’s readlink.

It is quite easy to use and available by default on every Linux distro. So just give a path of symbolic link with readlink command and that’s it.

readlink /path/to/symbolic/link

My symbolic link is placed at /home/sagar/Symbolics/MyTorrents so my command would be:

readlink /home/sagar/Symbolics/MyTorrents
How to Follow Symbolic Links

But what if your symbolic link involves multiple layers such as one link indicted to another? Well, in that case, you’d have to use -f option.

For this example, I’ve created a new symbolic link located at /home/sagar/Documents/NewLink and maps to the other link to have a better idea of how to deal with such scenarios:

readlink -f /home/sagar/Documents/NewLink
How to Follow Symbolic Links

2. Using realpath command

As its name suggests, the realpath utility is used to get the path of files and directories but the interesting thing is when used without any option, it can get us to the source of the symbolic link.

Using realpath even without any options is equivalent to using readlink -f so don’t worry about being mapped to another symbolic link.

The syntax of realpath to follow symbolic link to the source file is:

realpath /path/to/symbolic/link

And after specifying the path, end result should look like this:

How to Follow Symbolic Links

Learn Linux Quickly – Linux Commands for Beginners
Learn Linux Quickly doesn’t assume any prior Linux knowledge, which makes it a perfect fit for beginners. Nevertheless, intermediate and advanced Linux users will still find this book very useful as it goes through a wide range of topics. Learn Linux Quickly will teach you the following topics:Insta…
How to Follow Symbolic Links

3. Using stat command

The stat utility is used to get the status of files and can also be utilized to find the original source of the symbolic link.

Just give a path of the symbolic link to the stat command and that’s it.

stat /path/to/symbolic/link
How to Follow Symbolic Links

And if you find the other details unnecessary, you can use the -c%N option to filter them out. Not the easiest option to remember and hence use the man or help command to recall it.

stat -c%N /path/to/symbolic/link
How to Follow Symbolic Links

4. Using file command

Well, using the file command is quite easy and you’re required to follow the same syntax that you saw earlier with other examples.

A file command with a path to a symbolic link. That’s all you’d need!

file /path/to/symbolic/link
How to Follow Symbolic Links

Final Words

If you’re dealing with multilayer soft link layers, I recommend using the first two ways of following symbolic links.

These utilities are quite basic and do not require any complex syntax but if you’re still confused, let me know in the comments.

Microsoft Threat Intelligence Center Links Threat Group to Austrian Spyware Vendor DSRIF

Microsoft has linked the efforts of the threat group Knotweed to an Austrian spyware vendor. The group has so far used the malware dubbed ‘SubZero’ to attack groups in Europe and Central America. The Subzero malware, as used by Knotweed, can be used to hack a target’s phone, computers, network, and internet-connected devices.

DSRIF markets itself as a company that provides information research, forensics, and data-driven intelligence services to corporations. Yet, Microsoft has found multiple associations between the two apparently dissimilar groups which establishes a concrete link.

“These include command-and-control infrastructure used by the malware directly linking to DSIRF, a DSIRF-associated GitHub account being used in one attack, a code signing certificate issued to DSIRF being used to sign an exploit, and other open-source news reports attributing Subzero to DSIRF,” Microsoft said.

“Observed victims to date include law firms, banks, and strategic consultancies in countries such as Austria, the United Kingdom, and Panama.”

In 2021, the cyber mercenary group was also linked to the exploitation of a fourth zero-day, a Windows privilege escalation flaw in the Windows Update Medic Service (CVE-2021-36948) used to force the service to load an arbitrary signed DLL.

“To limit these attacks, we issued a software update to mitigate the use of vulnerabilities and published malware signatures that will protect Windows customers from exploits Knotweed was using to help deliver its malware,” said Cristin Goodwin, General Manager at Microsoft’s Digital Security Unit.

“We are increasingly seeing PSOAs selling their tools to authoritarian governments that act inconsistently with the rule of law and human rights norms, where they are used to target human rights advocates, journalists, dissidents and others involved in civil society,” Goodwin added.

The post Microsoft Threat Intelligence Center Links Threat Group to Austrian Spyware Vendor DSRIF appeared first on IT Security Guru.