Symbolic Link Operations

A symbolic link, also known as a symlink or a soft link, is a special kind of file (entry) that points to the actual file or directory on a disk (like a shortcut in Windows).

Symbolic links are used all the time to link libraries and often used to link files and folders on mounted NFS (Network File System) shares.

The ln command is a standard Linux utility for creation links:

ln -s <source> <link name>

Generally, the ln syntax is similar to the cp or mv syntax, e.g. <source> <destination>.

According to the man page, by default, each destination (<link name>) should not already exist.

If the path to the <link name> exists and it is a file, you will get an error like “ln: failed to create symbolic link ‘<link name>’: File exists”.

But if the path to the <link name> is an existent directory, link will be created inside that directory.

Create Soft Link

For example, create a symbolic link to a file and directory

ln -s <path to file> <path to link>
ln -s <path to dir> <path to link>

Delete Soft Link

There are 2 ways to undo or delete the soft link:

unlink <link name>
rm [-rf] <link name>

Note that, the rm command just removes the link. It will not delete the target directory.

Broken Soft Link

Find broken symbolic link:

find . -xtype l

If you want to delete in one go:

find . -xtype l -delete

A bit of explanation:
-xtype l tests for links that are broken (it is the opposite of -type)
-delete deletes the files directly, no need for further bothering with xargs or -exec

Resources:

SymLink – HowTo: Create a Symbolic Link – Linux
How can I find broken symlinks
How to delete broken symlinks in one go?

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章