Linux CLI HowTo 4 🐧 How To Symbolic and Hard links
4 – Symbolic and Hard links
1. How to Create a Symbolic Link to a File
Steps:
- Open terminal
- Navigate to desired directory where you want the link
- Type
ln -s /path/to/original/file linkname(e.g.,ln -s /home/test/file.txt mylink) - Verify with
ls -lato see the symbolic link
Why: Creates an alias that points to the original file without duplicating data
2. How to Create a Symbolic Link to a Directory
Steps:
- Open terminal
- Navigate to desired directory where you want the link
- Type
ln -s /path/to/original/directory linkname(e.g.,ln -s /home/test/documents docs) - Verify with
ls -lato see the symbolic link
Why: Creates a shortcut to access directories from different locations without copying them
3. How to Create a Hard Link to a File
Steps:
- Open terminal
- Navigate to directory containing the file
- Type
ln filename hardlinkname(e.g.,ln file.txt backup) - Verify with
ls -lato see both files pointing to same inode
Why: Creates another name for the same file data, useful for backup or multiple access points
4. How to Check if a File is a Symbolic Link
Steps:
- Open terminal
- Type
ls -la /path/to/file(e.g.,ls -la /home/test/mylink) - Look at the first character of output – ‘l’ indicates symbolic link
- Check the arrow pointing to original file
Why: Confirms file type and verifies the link relationship
5. How to Remove a Symbolic Link
Steps:
- Open terminal
- Type
rm linkname(e.g.,rm mylink) - Verify removal with
ls -laor try accessing the link
Why: Removes only the symbolic link, not the original file it points to
6. How to Remove a Hard Link
Steps:
- Open terminal
- Type
rm hardlinkname(e.g.,rm backup) - Verify with
ls -lathat original file still exists
Why: Removes only the link reference, not the actual file data (original remains)
7. How to List All Symbolic Links in a Directory
Steps:
- Open terminal
- Type
find /path/to/directory -type l(e.g.,find /home/test -type l) - Or use
ls -la | grep "^l"to filter only symbolic links
Why: Quickly identifies all symbolic links in a specific location
8. How to Follow Symbolic Links When Viewing File Contents
Steps:
- Open terminal
- Type
cat linkname(e.g.,cat mylink) - The content will be displayed from the original file that the link points to
Why: Accesses the actual file content through the symbolic link
9. How to Verify Hard Link Count
Steps:
- Open terminal
- Type
ls -la filename(e.g.,ls -la file.txt) - Look at the first number in output – this shows how many hard links exist
- Multiple files with same inode number indicate hard links
Why: Confirms how many references exist to the same file data
10. How to Create a Hard Link to a File in Another Directory
Steps:
- Open terminal
- Navigate to target directory where you want the hard link
- Type
ln /path/to/original/file filename(e.g.,ln /home/test/file.txt /tmp/backup) - Verify with
ls -lathat both files show same inode
Why: Creates a direct reference to file data in different locations without copying