LinuxCommandLibrary

ln

Create links between files

TLDR

Create a symbolic link to a file or directory

$ ln [[-s|--symbolic]] /[path/to/file_or_directory] [path/to/symlink]
copy

Overwrite an existing symbolic link to point to a different file
$ ln [[-sf|--symbolic --force]] /[path/to/new_file] [path/to/symlink]
copy

Create a hard link to a file
$ ln /[path/to/file] [path/to/hardlink]
copy

SYNOPSIS

ln [OPTION]... SOURCE [DEST]
ln [OPTION]... SOURCE... DIRECTORY
ln [OPTION]... -t DIRECTORY SOURCE...

PARAMETERS

-s, --symbolic
    Create symbolic links instead of hard links. This is the most common option when not creating hard links.

-f, --force
    Remove existing destination files or links that would otherwise prevent the link from being created.

-i, --interactive
    Prompt before overwriting existing destination files. Overrides -f.

-v, --verbose
    Print the name of each linked file.

-b, --backup[=CONTROL]
    Make a backup of any existing destination files before overwriting them. CONTROL can specify backup type (e.g., 'numbered', 'existing', 'none').

-d, --directory
    Allow superuser to make hard links to directories. Use with extreme caution as it can lead to filesystem loops and data loss.

-P, --physical
    Make hard links directly to physical files rather than through symbolic links if SOURCE is a symbolic link. This is the default for hard links.

-t, --target-directory=DIRECTORY
    Specify the DIRECTORY in which to create the links. This allows specifying the target directory before the sources.

DESCRIPTION

The ln command is used to create links between files or directories within a Linux filesystem. It supports two primary types of links:

Hard Links: A hard link is essentially another name or directory entry for an existing file. All hard links to a file point to the same inode (data block on disk). This means that if you modify the content of the file through one hard link, it affects all other hard links. Hard links have several characteristics: they must reside on the same filesystem as the original file, they cannot link to directories (by default, and generally discouraged even with superuser privileges), and deleting the original file does not delete the data itself until the last hard link is removed.

Symbolic Links (Soft Links): A symbolic link, often called a symlink or soft link, is a special type of file that contains a text string which is the path to another file or directory. It acts like a pointer or shortcut. Unlike hard links, symbolic links can span different filesystems and can link to directories. If the original file or directory pointed to by a symbolic link is moved or deleted, the symbolic link becomes 'broken' or 'dangling' and will no longer work. Symbolic links are commonly used to create aliases for frequently accessed files or directories, or to provide flexible file organization.

CAVEATS

Hard Links: Cannot span filesystems. Cannot link to directories (by default). Deleting the original file does not delete the data itself until the last hard link is removed.
Symbolic Links: Become 'broken' or 'dangling' if the target file/directory is moved, renamed, or deleted. This can lead to unexpected behavior if not managed properly.
Security: Creating arbitrary symbolic links can be a security risk (symlink attacks) if permissions are not properly managed, as they can be used to redirect file operations to unintended locations.

TYPES OF LINKS

The ln command primarily creates two types of links:
Hard Links: Direct pointers to the same inode on the filesystem. They share the same data and metadata as the original file. All hard links are equivalent; there is no 'original' once they are created.
Symbolic Links (Soft Links): These are special files that contain the path to another file or directory. They act like shortcuts. If the target is moved or deleted, the symlink will become broken (dangling).

INODES AND HARD LINKS

In Unix-like systems, files are identified by an inode number, which points to the actual data on disk. A hard link is simply an additional directory entry that points to an existing inode. When you create a hard link, the link count for that inode is incremented. The file's data is only truly deleted from disk when its link count drops to zero (i.e., all hard links, including the original file entry, are removed).

RELATIVE VS. ABSOLUTE SYMBOLIC LINKS

Symbolic links can store either an absolute path (e.g., /home/user/my_file) or a relative path (e.g., ../my_file). Relative links are generally more flexible and portable, as they remain valid even if the link and its target are moved together within the directory structure, as long as their relative positions remain the same. Absolute links are easier to manage if the target is in a fixed, well-known location.

HISTORY

The ln command has been a fundamental utility in Unix-like operating systems since their early days. Initially, it primarily supported the creation of hard links. Symbolic links were introduced much later, notably with 4.2 BSD in the early 1980s, to address the limitations of hard links, particularly the inability to link across filesystems or to directories. Modern versions of ln, such as those provided by GNU Coreutils, include numerous extensions and options for greater control over link creation, including backup options and verbose output.

SEE ALSO

rm(1), mv(1), cp(1), readlink(1), stat(1), find(1)

Copied to clipboard