Home > File & Directory Management > symlink

symlink: Create a Symbolic Link

The symlink command creates a symbolic link (soft link) to a file or directory. It acts as a pointer to the original file's location, and the link will be broken if the original file is deleted. While the `ln -s` command is more commonly used, the `symlink` utility offers fine-grained control over link creation through various additional options.

Overview

symlink creates a new entry (link) that points to a specific file or directory. This link stores the path to the original file, and any changes made to the original file's content will be reflected when accessed through the link. Symbolic links can also exist across different file systems.

Key Features

  • References the path of the original file/directory
  • Link becomes invalid (broken link) if the original is deleted
  • Can span across file system boundaries
  • Provides various options for controlling link creation

Key Options

The symlink command, similar to `ln -s`, creates symbolic links but offers various additional options for fine-grained control over the link creation process.

Basic Usage

Link Creation Control

Information Output

Generated command:

Try combining the commands.

Description:

`symlink` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Create a Symbolic Link to a File

echo "Hello World" > original_file.txt
symlink original_file.txt link_to_file.txt
cat link_to_file.txt

Creates a link named `link_to_file.txt` in the current directory that points to `original_file.txt`.

Create a Symbolic Link to a Directory

mkdir my_directory
symlink -d my_directory my_dir_link
ls -l

Creates a link named `my_dir_link` that points to `my_directory`.

Force Overwrite an Existing Link

echo "First content" > original_file.txt
symlink original_file.txt link_to_file.txt
echo "Second content" > new_original.txt
symlink -f new_original.txt link_to_file.txt
cat link_to_file.txt

Forcefully changes the existing `link_to_file.txt` to point to `new_original.txt`.

Create a Link Using an Absolute Path

echo "Absolute test" > test_file.txt
symlink -a test_file.txt absolute_link.txt
readlink absolute_link.txt

Creates a link by converting a relative path specified for the target to an absolute path.

Installation

The `symlink` command may not be included by default in all Linux distributions. On most modern Linux systems, it is more common to create symbolic links using `ln -s`. If you need the `symlink` utility, you can install it using the following commands.

Debian/Ubuntu

sudo apt update
sudo apt install symlink

Fedora/CentOS/RHEL (dnf)

sudo dnf install symlink

Note: If the `symlink` package is not available, it is recommended to use the `ln -s` command instead.

Tips & Precautions

Information on points to note when using `symlink` and more efficient alternatives.

  • Alternative Command: In most cases, the `ln -s` command is more widely used and feature-rich than `symlink`. Use it in the format `ln -s target link_name`.
  • Broken Links: If the original file or directory that a symbolic link points to is deleted, the link becomes a 'broken link' and is no longer valid. It will be displayed in red or indicate no target when checked with `ls -l`.
  • Relative vs. Absolute Paths: It is generally safer to specify the target path as an absolute path when creating a symbolic link. Using a relative path can cause the link's behavior to vary depending on where it was created, and the link may break if moved. The `symlink -a` option can help resolve this issue.

Same category commands