A link in Linux is a special file that refers to another file or directory. In this way, one file can be placed in several places at once. When the original file is deleted, the link to it is retained. This can result in crashes and generally clogs up the system.
To begin with, let's explain the method of checking that the file is actually a link. This is conveniently done using the ls
utility. Afterwards, we will consider three methods of removal, each of which has its own peculiarities.
With the help of ls
it is convenient to see information about the file. To use the utility, give it the -l
option and specify the path. As an example, let's take the file ~/symlinks/link_1
:
$ ls -l ~/symlinks/link_1
You can check an entire directory for links:
$ ls -l ~/symlin
A file that is not needed is marked with a characteristic arrow after its name.
The rm
command can be used to delete any file on the Linux file system, including links. That is, it is suitable for our task. It is important to mention here that the original file or directory will not be affected as a result of deleting the link.
In the terminal, execute the command:
$ rm ~/symlinks/link_1
Also, to get detailed information about the procedure, add the -v
option to the command:
$ rm -v ~/symlinks/link_1
Additionally, you can check whether the link has disappeared from the directory. To do this, use the previously described command ls
.
Now you know how to remove a Linux symbolic link.
The unlink
utility is designed to remove files from the Linux file system. It only needs to specify the path to the link:
$ unlink ~/symlinks/link_1
Within the scope of the task at hand, rm and unlink work almost identically. Now let's move on to the last method.
Using the find
command, you can search for and remove links in a given directory. This means that you don't need to use ls
additionally.
Unlike the previous two options, it has quite advanced features, such as setting search criteria. But the syntax is not so simple for this reason either.
To search, you need to specify a directory and a few parameters:
$ find ~/symlinks/ -type l -print
The -type l
parameter is needed to select only symbolic links, -print
to output the full name. And with the help of -maxdepth
you can specify the depth of search by nested directories. If you specify 1, the search will be performed only on the directory itself, without attachments:
$ find ~/symlinks/ -maxdepth 1 -type l -print
Once there are many links in the directory, you can specify a search pattern for the find
command. As an example, let's take a situation where we want to find only hidden links with a last modified date no more than 30 days ago:
$ find ~/data/ -type l -name ".*" -mtime -30 -print
Two criteria have been added:
-name
- search by name
.-mtime
- by modification time.All found links can be deleted according to the specified pattern. For this purpose -print
is replaced by -delete
:
$ find ~/data/ -type l -name ".*" -mtime -30 -delete
At the end of the instruction we will deal with searching and deleting broken links that do not lead anywhere, for example, after deleting the original file or directory. For this purpose, the -xtype
l
parameter will be suitable:
$ find ~/broken_links/ -xtype l
As a result, all broken links will be displayed in the terminal. To remove them, add -delete
at the end of the command:
$ find ~/broken_links/ -xtype l -delete
This completes the instructions for deleting a link in Linux.