ln
ln linux command cheatsheet by Thamizhiniyan C S
Introduction
The ln
command is used to create hard links and soft links for files in Linux.
Syntax
ln [OPTION]... [-T] TARGET LINK_NAME
ln [OPTION]... TARGET
ln [OPTION]... TARGET... DIRECTORY
ln [OPTION]... -t DIRECTORY TARGET...
Important Flags
Flag | Description |
---|---|
| make a backup of each existing destination file |
| like --backup but does not accept an argument |
| allow the superuser to attempt to hard link directories (note: will probably fail due to system restrictions, even for the superuser) |
| remove existing destination files |
| prompt whether to remove destinations |
| dereference TARGETs that are symbolic links |
| treat LINK_NAME as a normal file if it is a symbolic link to a directory |
| make hard links directly to symbolic links |
| with -s, create links relative to link location |
| make symbolic links instead of hard links |
| override the usual backup suffix |
| specify the DIRECTORY in which to create the links |
| treat LINK_NAME as a normal file always |
| print name of each linked file |
| display this help and exit |
| output version information and exit |
Hard Link
Hard links are the original Unix method of creating links.
Every file has at least one hard link by default, which gives it a name.
Creating a hard link adds another directory entry for the same file.
Hard links are indistinguishable from the original file itself in directory listings.
When a hard link is deleted, the link itself is removed, but the file's contents remain until all links to it are deleted.
Limitations of hard links:
Cannot reference a file outside its own file system (same disk partition).
Cannot reference a directory.
Soft Link (aka Symbolic Link )
Symbolic links (symlinks) were introduced to overcome the limitations of hard links.
They create a special type of file that contains a text pointer to the referenced file or directory.
Similar to Windows shortcuts, symlinks allow referencing files or directories.
A symbolic link and the file it points to are almost indistinguishable; operations on the symlink affect the referenced file.
Deleting a symbolic link removes only the link itself, not the referenced file.
If the referenced file is deleted, the symlink becomes broken, pointing to nothing.
Many implementations of the
ls
command display broken symlinks in a distinguishing color (e.g., red) to indicate their presence.
Soft Links vs. Hard Links
Source: https://phoenixnap.com/kb/symbolic-link-linux
Category | Soft Link (aka Symbolic Link ) | Hard Link |
---|---|---|
Reference Type | Follows a path to a file or a directory. | Points to the actual file data on the storage volume. |
Compatible Targets | Uses files and directories on local and external volumes. | Can be created only locally as it references a physical location on the volume |
Target Dependence | Does not work after its target file is moved or deleted. | Works after the target file is moved or deleted. |
Usage | Offers quick access to a frequently-used file. | Provides flexibility when organizing a filesystem. |
Examples
Command | Description |
---|---|
To create a hard link to a file | |
To create a symbolic link to a file | |
Creates a symbolic link (link-file.txt) that points to the target file (target-file.txt) located in the test directory. | |
To create a symbolic link to a directory | |
Overwriting Symbolic Links. Using the -f option permanently deletes the existing file. | |
Removing the symbolic link | |
Removing the symbolic link | |
To perform the search and locate the links that do not work (Find all Broken Symbolic Links) and remove them |
Last updated