Recognize symbolic links

Lesson 10 of 30 · Working with Linux

A symbolic link is a filesystem entry that stores a path to a target. It provides another name for the target, not a second copy of its contents. Return to the practice directory and confirm it with pwd. Create a fresh subdirectory named link-demo; if creation fails or the name exists, stop and choose a different unused name. Enter it only after creation succeeds, and confirm your location with pwd. Then create a link to the practice file one directory above:

mkdir link-demo
cd link-demo
pwd
ln -s ../field.txt current-notes
ls -l current-notes
cat current-notes

ln -s takes the target path first and the new link name second. The listing includes current-notes -> ../field.txt; other details vary. Reading current-notes follows the link and displays the same four lines as field.txt. The link does not duplicate the file: changes to the target are visible through the link.

The stored target ../field.txt starts from the link’s directory, even when a command follows the link from somewhere else. Because this example reaches up to the practice directory, moving link-demo by itself would change that relationship and could leave the link dangling. Moving the whole practice directory keeps the link and file in the same relative positions. A dangling link still has a directory entry, but its target cannot be reached; an absolute target instead starts at /. Removing a link removes that entry, not the file it refers to; there is no need to remove it here. If link creation fails, stop and inspect the error.