KandZ – Tuts

We like to help…!

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:

  1. Open terminal
  2. Navigate to desired directory where you want the link
  3. Type ln -s /path/to/original/file linkname (e.g., ln -s /home/test/file.txt mylink)
  4. Verify with ls -la to 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:

  1. Open terminal
  2. Navigate to desired directory where you want the link
  3. Type ln -s /path/to/original/directory linkname (e.g., ln -s /home/test/documents docs)
  4. Verify with ls -la to 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:

  1. Open terminal
  2. Navigate to directory containing the file
  3. Type ln filename hardlinkname (e.g., ln file.txt backup)
  4. Verify with ls -la to 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:

  1. Open terminal
  2. Type ls -la /path/to/file (e.g., ls -la /home/test/mylink)
  3. Look at the first character of output – ‘l’ indicates symbolic link
  4. Check the arrow pointing to original file

Why: Confirms file type and verifies the link relationship


5. How to Remove a Symbolic Link

Steps:

  1. Open terminal
  2. Type rm linkname (e.g., rm mylink)
  3. Verify removal with ls -la or 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:

  1. Open terminal
  2. Type rm hardlinkname (e.g., rm backup)
  3. Verify with ls -la that 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:

  1. Open terminal
  2. Type find /path/to/directory -type l (e.g., find /home/test -type l)
  3. 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:

  1. Open terminal
  2. Type cat linkname (e.g., cat mylink)
  3. 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:

  1. Open terminal
  2. Type ls -la filename (e.g., ls -la file.txt)
  3. Look at the first number in output – this shows how many hard links exist
  4. 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:

  1. Open terminal
  2. Navigate to target directory where you want the hard link
  3. Type ln /path/to/original/file filename (e.g., ln /home/test/file.txt /tmp/backup)
  4. Verify with ls -la that both files show same inode

Why: Creates a direct reference to file data in different locations without copying

Leave a Reply