Hard and Soft Links, Explained

UPI Study › Courses › Introduction to Operating Systems › Free lessons › Lesson 6.6 Free with a free account

Hard and Soft Links

Introduction to Operating Systems · Chapter 6: File Systems and Data ManagementSection 6.6 · lesson 53 of 836 min · free with a free account

← File ExtensionsNext lesson: File Permissions →

A free account plays all of Chapter 1 and 3 more lessons of Introduction to Operating Systems. Create a free account →

Earn 3 college credits for Introduction to Operating SystemsRecommended for credit by NCCRS · Computer Science 354 · self-paced · one payment, lifetime access

In this lesson

How hard links share an inode and symbolic links store a path.

  • Distinguish a hard link from a symbolic link in terms of inodes and paths
  • Create hard and symbolic links with ln and ln -s and inspect them with stat and readlink
  • Predict what happens to each kind of link when the original name is deleted or renamed
  • Explain why hard links usually cannot cross file systems or point to folders

Free study notes

Get this lesson's notes by email

The key points and 4 practice questions with answers, in your inbox. One email, free.

One email. Unsubscribe in one click.

Transcript

Everything said in the video, section by section (938 words). Read along, or search it.

Opening Problem

One Recipe Reached From Three Places

Sam, who runs a small online bakery, keeps one very important file: the master recipe. And Sam wants to reach it from three places: the kitchen folder, the shop folder, and the desktop. Copying it three times is a trap. An edit to one copy never reaches the other two. What Sam wants is one real file, with several ways to reach it. Operating systems offer exactly that, in two flavors. They look alike on screen, but they behave very differently when something is deleted.

Two Kinds

Catalog Cards and See-Also Notes

Think of a library catalog. A book can have one card under its title, and another under its author. Both lead to the same book on the shelf. Some catalogs also have see-also cards, which point to another card instead of to the book. Those are the two kinds of link. A hard link is a second name that leads straight to the same file: the same inode, from lesson six point two. A symbolic link, also called a symlink or soft link, is a small file of its own that holds a path: a see-also note that says where to look.

Hands-On

Creating Links With ln and ln -s

Let's try both on this Mac. Write the recipe into recipe dot t x t. ln, recipe dot t x t, hard dot t x t, makes a hard link. ln dash s, recipe dot t x t, soft dot t x t, makes a symbolic link. stat dash f prints the facts we ask for, for all three: the inode number, the mode, the link count, the size, and the name.

Reading stat

Shared Inodes and Link Counts

Look at the inode numbers. recipe dot t x t and hard dot t x t share one: two names, one file. Your numbers will differ, but the two will match. And the link count after the permissions is two. There's no original and no copy. The two names are equal in every way. soft dot t x t is different. Its mode starts with l, for link. It has its own inode, a count of one, and an arrow to recipe dot t x t. Its size is ten bytes: the ten characters of the name recipe dot t x t. That name is the whole note it holds.

Shared Edits

One File Changed Through Any Name

readlink prints that note: recipe dot t x t. Now add a line through one name: echo and butter, with two greater-than signs, into recipe dot t x t. Then read hard dot t x t. The new line is there too, because there's only one file to change.

Quick Check

Deleting the Original Name

Quick check. Sam now deletes the name recipe dot t x t, with rm. Then Sam reads hard dot t x t, and soft dot t x t. Does only the hard link still work, do both still work, or do both fail? Pause and predict. hard dot t x t still prints the recipe, and soft dot t x t fails: no such file or directory. rm removed one name. The inode still had another name, hard dot t x t, so its data stayed. But soft dot t x t only remembers a path, and nothing has that name any more. It's a dangling link, also called a broken link. Both still work is the tempting answer. But a symbolic link never holds the data, only the way to it.

After Deletion

Link Counts and Dangling Links

Ask stat again. hard dot t x t now has a link count of one. And soft dot t x t is still there, pointing at a name that's gone. When the last name goes, and no program has the file open, its space is freed.

Limits

Where Hard Links Cannot Go

Hard links have limits. They normally can't cross from one file system to another, because an inode number only makes sense inside its own file system. And most systems refuse hard links to folders, since that could create loops in the tree. A symbolic link can point at a folder, at another file system, or even at a name that doesn't exist yet. Windows shortcuts and Mac aliases are close cousins of symbolic links. A Mac alias can even find an original that has moved.

Real Example

A Current Term Folder Link

Ivy, who looks after the computers at a school, puts that to work. Make a folder for each term, each with its own timetable. A hard link to the fall folder is refused: ln says it's a directory. A symbolic link called current term works. Through it, cat reads the fall timetable. At the new term, ln dash s f n points the same link at the spring folder. Same address, new folder, and nobody has to learn a new path.

Quick Check

Renaming a Symlink's Target Folder

Quick check. Ivy tidies up, and renames the folder spring twenty twenty-seven to spring term. Then a teacher opens current term slash timetable dot t x t. Does it show the spring timetable, the fall timetable, or an error? Pause and predict. An error. current term still holds the path spring twenty twenty-seven, and nothing has that name now. A symbolic link breaks when its target is moved, renamed or deleted, because it only remembers a path. The fix is to point it again, with ln dash s f n. The spring timetable is the tempting answer, but the link doesn't follow the folder. When a link stops working, check its target first.

Key Takeaways

Hard Links Share, Symlinks Point

To recap. A hard link is a second directory entry for the same inode, so the link count goes up. A symbolic link is a small file that holds a path, with its own inode. Delete the original name, and a hard link still works, while a symbolic link dangles. Symbolic links can reach folders and other file systems. Hard links normally can't.

About Introduction to Operating Systems at UPI Study

Introduction to Operating Systems (Computer Science 354) is a self-paced online course of 83 video lessons in 10 chapters, from what an operating system does and process scheduling to concurrency, memory, devices, file systems, networks, the internet, security and virtual machines. It carries a NCCRS credit recommendation for 3 semester credits.

Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Compare: Linux vs Operating Systems

Why take it here

Why students take Introduction to Operating Systems with UPI Study

What happens under every app you runIntroduction to Operating Systems explains the software that runs a computer: processes and scheduling, threads and synchronization, deadlock, memory and paging, devices and storage, file systems, networking, security and virtualization. It suits computer science and information technology degrees.
Credit at a fraction of the priceThe same 3 credits can cost $750 or more at a US college. Here it is one payment, self-paced, with lifetime access.
Finish your degree soonerTransfer credits reduce the courses you still owe, so you can graduate earlier or lighten a heavy semester.
Strengthen an applicationA completed college-level course on an official transcript shows admissions and scholarship committees you can do the work.
Study on your own scheduleNo start dates, no deadlines. Watch the lessons, practice, and take the final when you are ready.
130,000+students helped with college creditsACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Where the credit goes

Universities that work with NCCRS

Introduction to Operating Systems carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.

FAQ

Questions students ask us

About Hard and Soft Links

What is the difference between a hard link and a symbolic link?
A hard link is a second directory entry for the same inode, while a symbolic link is a small separate file that stores a path to another name. Hard-linked names are equal, with no original and no copy, and each one raises the inode's link count. A symbolic link has its own inode and simply tells the system where to look.
What happens to a hard link when the original file is deleted?
The hard link keeps working, because deleting one name removes that directory entry and lowers the inode's link count. As long as another name still points to the inode, the data stays on the drive. The space is freed when the last name is removed and no program still has the file open, so the order in which names are deleted does not matter.
What is a dangling symbolic link?
A dangling symbolic link, also called a broken link, points to a path that no longer exists. Because a symlink stores a path rather than the data, it breaks when its target is deleted, moved or renamed. Opening it gives an error such as no such file or directory. The fix is to point it at the right place again, for example with ln -sfn.
Can you create a hard link to a directory?
Usually not, because most systems refuse hard links to directories, since they could create loops in the folder tree. Running ln on a folder typically fails with a message that it is a directory. A symbolic link can point at a folder, and it can also cross into another file system, which a hard link normally cannot do.

About the Introduction to Operating Systems course

How many college credits is Introduction to Operating Systems?
Introduction to Operating Systems (Computer Science 354) carries a NCCRS credit recommendation for 3 semester credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
How much does Introduction to Operating Systems cost and what is included?
$250, paid once, with lifetime access: all 83 video lessons, a practice quiz after each, the graded work and final exam, and an official transcript when you finish. Students who want several courses choose UPI Study PRO instead.
Which universities accept UPI Study credits?
UPI Study has signed partner agreements with universities such as Thomas Edison State University, University of the People and Charter Oak State College, and works with universities such as Penn State, SNHU and SUNY. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities. Use "Find my college" on this page to check yours.
How long does Introduction to Operating Systems take?
It is self-paced with no deadlines. The 83 lessons total about 10 hours of video; most students add reading and practice time and finish in a few weeks to a few months, on their own schedule.
How are the exams done?
Quizzes and assignments are online in the course. The final exam is taken online with proctoring; a person on the UPI Study team checks assignments, usually within two days.
How do my credits reach my college?
When the course shows 100% complete, request the official transcript from your dashboard. It is sent to the college you name, and you can send it to more than one. Check your college first with "Find my college" on this page, or ask Athena.

Ready for the full course? Earn 3 credits in Introduction to Operating Systems.

83 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.

Updated

Introduction to Operating Systems
3 credits · NCCRS
Enroll →