CIS120 Linux Fundamentals by Scott Shaper

Creating Symbolic and Hard Links

Think of links in Linux like shortcuts on your desktop, but more powerful. They let you access the same file from different locations without making copies. There are two types: hard links and symbolic (soft) links.

Why Use Links?

Links are useful when you want to:

Hard Links

A hard link is like having multiple names for the same file. Think of it as a person with a nickname - both names refer to the same person. When you create a hard link, both the original file and the link point to the same data on your hard drive.

How Hard Links Work

In Linux, every file has an inode number - a unique identifier that points to the actual data on the disk. When you create a hard link:

Creating a Hard Link

ln original_file link_name

For example:

ln notes.txt notes_backup.txt

Important Things to Know About Hard Links

Symbolic Links (Soft Links)

A symbolic link is like a shortcut on your desktop. It points to the original file's location. If you move or delete the original file, the link stops working.

Creating a Symbolic Link

ln -s original_file link_name

For example:

ln -s notes.txt notes_shortcut.txt

Important Things to Know About Symbolic Links

Practical Examples

Creating a Hard Link

Let's say you have a file called project.txt and want to access it from another location:

ln project.txt /home/user/documents/project_backup.txt

Creating a Symbolic Link

To create a shortcut to a frequently used directory:

ln -s /home/user/documents/project /home/user/desktop/project_shortcut

Understanding Paths

When creating links, you need to understand two types of paths:

Absolute Paths

These start from the root directory (/) and show the complete path:

ln -s /home/user/documents/file.txt /home/user/desktop/file_link.txt

Relative Paths

These are based on your current location. For example, if you're in your home directory:

ln -s documents/file.txt desktop/file_link.txt

Important note about relative paths in symbolic links:

You can also create links that go up directories using ..:

# If you're in /home/user/projects/current
# and want to link to a file in /home/user/documents
ln -s ../../documents/notes.txt notes.txt

# If you're in /home/user/projects/current/subfolder
# and want to link to a file in /home/user/documents
ln -s ../../../documents/notes.txt notes.txt

What Happens When Source Files Are Deleted

Hard links and symbolic links behave very differently when the original file is deleted:

Tips for Success

Common Mistakes to Avoid