ln -s


ln -s (symbolic link)

The ln -s command creates a symbolic link (also known as a soft link or symlink). Unlike a hard link, a symbolic link does not point directly to the file's data (inode). Instead, it is a special type of file that contains a path to the target file or directory.


Example 1: Create a symbolic link to a file

# Create a symbolic link named 'slink_log' that points to '/var/log/messages'
ln -s /var/log/messages slink_log

Explanation: This creates a new file slink_log that acts as a pointer or shortcut to /var/log/messages. Any operation on slink_log (like cat or less) is redirected to the target file.


Example 2: Create a symbolic link to a directory

# Create a symbolic link named 'my_app' that points to the directory '/opt/long/path/to/app'
ln -s /opt/long/path/to/app my_app

Explanation: Symbolic links are commonly used to create convenient shortcuts to deeply nested directories. You can cd my_app and it will take you to the target directory.


Example 3: Identifying a symbolic link with ls -l

# List the details of the link to see its type and where it points
ls -l slink_log

Explanation: The output of ls -l clearly identifies a symbolic link. The first character of the permissions string will be an l, and the listing will show the link name followed by -> and the path to the target it points to.


Example 4: The effect of a broken symbolic link

# Rename the original target file
mv main.c main.bak
# Now a symlink pointing to main.c is "broken"
ln -s main.c main_link
ls -l main_link

Explanation: If the target file is moved, renamed, or deleted, the symbolic link is not automatically updated. It becomes a "broken" link, pointing to a path that no longer exists. On many systems, ls will highlight broken links in red.


Example 5: Symbolic links across different filesystems

# Create a symlink in a user's home directory pointing to a file on a mounted USB drive
ln -s /mnt/usb/document.pdf ~/usb_doc

Explanation: A key advantage of symbolic links is that they can point to files or directories anywhere, including across different filesystems or mounted network shares. This is because they simply store a text-based path, not an inode number.


Example 6: Overwriting an existing symbolic link

# Force the creation of a new symbolic link, replacing the old one if it exists
ln -sf /new/target/path my_app

Explanation: Combining -s (symbolic) with -f (force) allows you to create a symbolic link even if a file or link of the same name already exists. The existing item will be removed and replaced by the new symbolic link.


Example 7: Using relative paths for symbolic links

# In the directory /home/user/project, create a link to a file in a sibling directory
# Current directory: /home/user/project
ln -s ../configs/app.conf ./config

Explanation: This creates a symbolic link named config that points to ../configs/app.conf. Using a relative path makes the link portable; if you move the parent directory (project), the link will not break as long as the relative structure is maintained.