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

FlagDescription

--backup[=CONTROL]

make a backup of each existing destination file

-b

like --backup but does not accept an argument

-d, -F, --directory

allow the superuser to attempt to hard link directories (note: will probably fail due to system restrictions, even for the superuser)

-f, --force

remove existing destination files

-i, --interactive

prompt whether to remove destinations

-L, --logical

dereference TARGETs that are symbolic links

-n, --no-dereference

treat LINK_NAME as a normal file if it is a symbolic link to a directory

-P, --physical

make hard links directly to symbolic links

-r, --relative

with -s, create links relative to link location

-s, --symbolic

make symbolic links instead of hard links

-S, --suffix=SUFFIX

override the usual backup suffix

-t, --target-directory=DIRECTORY

specify the DIRECTORY in which to create the links

-T, --no-target-directory

treat LINK_NAME as a normal file always

-v, --verbose

print name of each linked file

--help

display this help and exit

--version

output version information and exit


  • 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.


  • 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.


Source: https://phoenixnap.com/kb/symbolic-link-linux

CategorySoft 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

CommandDescription
ln [target/file] [symlink]

To create a hard link to a file

ln -s [target/file] [symlink]

To create a symbolic link to a file

ln -s test/target-file.txt link-file.txt

Creates a symbolic link (link-file.txt) that points to the target file (target-file.txt) located in the test directory.

ln -s [target-directory] [symlink]

To create a symbolic link to a directory

ln -sf [target] [destination]

Overwriting Symbolic Links. Using the -f option permanently deletes the existing file.

unlink [symlink/link_file]

Removing the symbolic link

rm [symlink/link_file]

Removing the symbolic link

find [directory] -type l ! -exec test -e {} \; -print | while read line; do unlink "$line"; done

To perform the search and locate the links that do not work (Find all Broken Symbolic Links) and remove them

Last updated