0
0
Linux CLIscripting~5 mins

ln (hard and symbolic links) in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to have multiple names or shortcuts for the same file without copying it. The ln command creates links that point to the original file, saving space and making file management easier.
When you want two filenames to refer to the exact same file data on disk without duplicating it.
When you want a shortcut to a file in another directory that updates if the original file changes.
When you want to organize files in different folders but keep them linked to a single source.
When you want to save disk space by avoiding duplicate copies of large files.
When you want to create a backup reference to a file that stays updated automatically.
Commands
Create a simple text file named original.txt with the content 'Hello World'. This file will be used to demonstrate links.
Terminal
echo "Hello World" > original.txt
Expected OutputExpected
No output (command runs silently)
Create a hard link named hardlink.txt pointing to original.txt. Both files share the same data on disk.
Terminal
ln original.txt hardlink.txt
Expected OutputExpected
No output (command runs silently)
Create a symbolic (soft) link named symlink.txt that points to original.txt. This is like a shortcut to the file.
Terminal
ln -s original.txt symlink.txt
Expected OutputExpected
No output (command runs silently)
-s - Create a symbolic link instead of a hard link
List the files with inode numbers and details to show how hardlink.txt shares inode with original.txt, but symlink.txt has a different inode.
Terminal
ls -li original.txt hardlink.txt symlink.txt
Expected OutputExpected
123456 -rw-r--r-- 2 user user 12 Jun 10 12:00 original.txt 123456 -rw-r--r-- 2 user user 12 Jun 10 12:00 hardlink.txt 123457 lrwxrwxrwx 1 user user 12 Jun 10 12:00 symlink.txt -> original.txt
-l - Show detailed file information
-i - Show inode number
Change the content of hardlink.txt. Since it is a hard link, original.txt content changes too.
Terminal
echo "New content" > hardlink.txt
Expected OutputExpected
No output (command runs silently)
Display the content of original.txt to confirm it changed after editing hardlink.txt.
Terminal
cat original.txt
Expected OutputExpected
New content
Remove the original.txt file. The hard link still keeps the data accessible.
Terminal
rm original.txt
Expected OutputExpected
No output (command runs silently)
Show that hardlink.txt still contains the data even after original.txt was deleted.
Terminal
cat hardlink.txt
Expected OutputExpected
New content
Try to read symlink.txt after original.txt is deleted. It will fail because the symbolic link points to a missing file.
Terminal
cat symlink.txt
Expected OutputExpected
cat: symlink.txt: No such file or directory
Key Concept

If you remember nothing else from this pattern, remember: hard links share the same file data and inode, while symbolic links are shortcuts that can break if the original file is removed.

Common Mistakes
Trying to create a hard link to a directory.
Hard links to directories are not allowed because they can break the file system structure.
Use symbolic links (-s) when linking directories.
Deleting the original file and expecting the symbolic link to still work.
Symbolic links point to the file name, so if the original file is deleted, the link breaks.
Keep the original file or use hard links if you want the data to persist.
Confusing hard links and symbolic links and their behavior.
Hard links share the same inode and data, symbolic links are separate files pointing to the original path.
Understand the difference and choose the right link type for your need.
Summary
Use ln to create hard links that share the same file data and inode.
Use ln -s to create symbolic links that act as shortcuts to files or directories.
Hard links keep data accessible even if the original file is deleted; symbolic links break if the original is removed.