Understanding Symbolic Links: Deletion, Path Issues, and Best Practices

When deleting a symbolic link, are you deleting the symbolic link itself or the file/directory it points to?

Experience Log

Create a symbolic link using an absolute path

symbolic link

A symbolic link created using a relative path will become invalid if the path of the symbolic link changes. In the image, after moving softLink up one directory, the link turns red and the real path it points to cannot be found.

However, a symbolic link created using an absolute path will not have this issue.

Do not include a trailing slash / when deleting a symbolic link

symbolic link

When deleting, since the symbolic link itself points to another directory, using tab completion will automatically add a trailing slash!!!!

So, when deleting a symbolic link, make sure not to include a trailing slash! There are many painful examples online.

Do not include a trailing slash / when moving a symbolic link

Similar to the deletion case, adding a trailing slash will cause the move to fail.

mv softLink/* ../ means moving all files in the linked directory.

The ../ in a symbolic link is not the parent directory of the symbolic link node

When we move softLink to the tmp directory and then enter the symbolic link, returning to the parent directory of the symbolic link is /tmp, not the parent directory of targetDir.

View the real path within a symbolic link

pwd -P

find does not enter the directory linked by the symbolic link by default

Other: When creating a symbolic link, adding or not adding a “/” at the end of the source directory only affects the display of an extra “/” in ls -l, with no other impact.