Skip to Content
This project is a work in progress. If you have any questions or suggestions, feel free to contact me.

What is a Link in Linux?

A link is a reference to a file. There are two types:

  • Points directly to the inode (actual data).
  • File and hard link are indistinguishable.
  • If original is deleted, the data remains (as long as one link exists).
  • Cannot span different filesystems or link to directories (by default).
  • Points to another filename (like a shortcut).
  • Can link across filesystems and to directories.
  • Breaks if the target is removed or renamed.

Syntax:

ln [source_file] [hard_link_name]

Example:

ln file.txt file_hard.txt
  • Creates a hard link named file_hard.txt pointing to the same data as file.txt.
ls -l
  • Look at the second column – shows number of links to the file’s inode.

Syntax:

ln -s [target] [link_name]

Example (file):

ln -s file.txt file_symlink.txt

Example (directory):

ln -s /var/log logs

Use ls -l:

ls -l

Output Example:

-rw-r--r-- 2 user user 512 Jul 22 file.txt -rw-r--r-- 2 user user 512 Jul 22 file_hard.txt lrwxrwxrwx 1 user user 9 Jul 22 file_symlink.txt -> file.txt
  • Hard link: same inode count, identical size
  • Symlink: shows -> pointing to the target
FeatureHard LinkSymbolic (Soft) Link
Points toInode (actual data)File name (path)
Survives original?YesNo (broken link if deleted)
Across filesystems?NoYes
Can link directories?No (by default)Yes
Commandlnln -s
  • Removing symlink:

    rm file_symlink.txt
  • Removing hard link:

    rm file_hard.txt

    (Original data exists as long as at least one link remains)

Summary

TaskCommandNotes
Create a hard linkln source_file hard_linkCannot be used on directories (default)
Create a symbolic (soft) linkln -s target_path symlink_nameWorks for files and directories
Create symlink to a fileln -s file.txt link.txtlink.txt points to file.txt
Create symlink to a directoryln -s /var/log logslogs points to /var/log
List all links with detailsls -lSymbolic links show -> target
View inode numbers (verify hard links)ls -liSame inode = hard link
Check link type with filefile filenameIdentifies symlink, text, binary, etc.
Remove a symbolic linkrm symlink_nameOnly deletes the link, not the target
Remove a hard linkrm hard_linkData stays if at least one link remains
Last updated on