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
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Introduction to Operating Systems.
Lesson complete
Earn the 3 college credits for Introduction to Operating Systems
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
Next lesson: File Permissions →← 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 →
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.
Enroll now → Course details Download the syllabus All Computer Science courses Every course with PRO →
Compare: Linux vs Operating Systems
Why take it here
Why students take Introduction to Operating Systems with UPI Study
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.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →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?
What happens to a hard link when the original file is deleted?
What is a dangling symbolic link?
Can you create a hard link to a directory?
About the Introduction to Operating Systems course
How many college credits is Introduction to Operating Systems?
How much does Introduction to Operating Systems cost and what is included?
Which universities accept UPI Study credits?
How long does Introduction to Operating Systems take?
How are the exams done?
How do my credits reach my college?
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
3 credits · NCCRSEnroll →