LearnLinux FoundationsManipulation

ln

ln - a hands-on Linux lab on a real virtual machine.

Give one file a second name with ln, a hard link: same inode, same data, no copy. ln is silent on success, so you prove the link with ls -i (both names share one inode number) and the link count in ls -l column 2. Write through one name and read it through the other to feel that it is one file. Learn -v and -f, and the two walls a hard link cannot cross: directories and other disks, which is what ln -s is for.

Here is something that should not be possible. You have one file. It holds one copy of your data, taking up space once on the disk. Now you are going to give that single file a SECOND name, in a second place, without copying a single byte. Edit either name and both change, because there is only ever one file underneath.

That is a hard link, and the command that makes one is ln. It is how the system lets one file live under two names at once. This lesson shows you how to create that second name, and, just as important, how to prove the two names really are one file.

The black boxes below are a practice terminal: a safe sandbox that checks the one command each step teaches, so you cannot break anything. ln is silent on success, so it prints nothing when it works. That is why every drill runs ln, then runs a second command to make the invisible result visible. The one number that changes machine to machine is the inode number; the fact that BOTH names share the SAME number is true everywhere, and that is the whole point.

ln is short for link. Its basic job is to create a hard link: a new name that points at an existing file's data. Not a copy. Not a shortcut. A genuine second name for the exact same file.

To understand it, you need one idea: the inode. Every file on a Linux disk has a hidden ID number called its inode. The inode is the real file: it holds the data and the record of where that data sits on disk. A filename is just a label pointing at an inode. Usually one label points at one inode. ln lets you attach a second label to the same inode, so two names lead to one file.

The shape is ln <existing-file> <new-name>. The first name must already exist; the second is the new name you are creating for it.

Hard links are as old as Unix itself, from the early 1970s. The design came first and the shortcut-style symbolic link came years later. That order is why ln with no flags makes the original kind, the hard link, and you add -s for the newer symbolic one. You will meet ln -s in the very next lesson; this lesson is the hard link.

Start with a file that already exists. Imagine original.txt sitting in your folder. You want a second name for it, hardlink.txt, pointing at the same data.

Run ln and, because it is silent, expect NO output at all. A blank line and a fresh prompt means it worked:

ln original.txt hardlink.txt

prompt: student@linuxcamp:~$ answer: ln original.txt hardlink.txt output: hint: Type ln, the existing file, then the new name: ln original.txt hardlink.txt

Nothing printed, and that is correct. ln says nothing when it succeeds, exactly like the copy and move commands you met earlier. The new name hardlink.txt now exists and points at the same data as original.txt. But you should never trust a silent command on faith. Next you PROVE the two names are one file, by reading the one thing that gives it away: the inode number.

Every file's inode number is printable with ls -i. The -i flag adds the inode ID as the first thing on each line. If two names share one inode number, they are not copies. They are one file wearing two labels.

Ask ls -i for both names at once and compare the numbers:

ls -i original.txt hardlink.txt

prompt: student@linuxcamp:~$ answer: ls -i original.txt hardlink.txt output: 812457 hardlink.txt 812457 original.txt hint: Use ls with the -i flag, then both filenames: ls -i original.txt hardlink.txt

The number 812457 is this machine's inode for that file. On YOUR machine the number will be different. What is identical everywhere, and what actually matters, is that the SAME number appears next to BOTH names. That match is the proof: two labels, one inode, one file. If the numbers differed, you would have two separate files, not a hard link.

Both names carry the inode 812457, so they are the same file. This is the difference between a hard link and a copy. A copy would have its own separate inode and its own separate data, doubling the disk space. The hard link adds a name and nothing else: no new data, no new inode, no extra space. You just proved it with your own eyes, using the one number that cannot lie.

There is a second way to see the link, and it is one you have already seen without knowing what it meant. Run ls -l, the long listing, and look at the second column, right after the permissions. That number is the link count: how many names point at this file's inode.

A normal file has a link count of 1, one name. After your ln, the file has two names, so the count reads 2. Look at it now:

ls -l original.txt

prompt: student@linuxcamp:~$ answer: ls -l original.txt output: -rw-r--r-- 2 student student 14 Jul 5 10:22 original.txt hint: Use ls -l on the file and read the number after the permissions: ls -l original.txt

That 2, the second field on the line, is the link count. It says two names now point at this one inode. Before your ln it read 1. This is the same second column you have glanced past in every ls -l since the navigation module; now you know what it counts. Make a third hard link and it would climb to 3. Delete a name and it drops back down. The count is the file's own record of how many labels are attached to it.

The inode match and the link count both say the two names are one file. Now feel it. If you change the data through one name, the other name must show the change too, because there is only one set of data underneath.

Write a line into original.txt, then read it back through hardlink.txt. You are writing to one name and reading from the other:

echo hello > original.txt; cat hardlink.txt

prompt: student@linuxcamp:~$ answer: echo hello > original.txt; cat hardlink.txt output: hello hint: Send text into original.txt, then cat the other name: echo hello > original.txt; cat hardlink.txt

You wrote hello into original.txt, but reading hardlink.txt printed it right back. That is the whole idea made real. The two names are not two files kept in sync; they are one file with two doors. Write through either door and the data changes for both, because there is nothing to keep in sync. There is only one file. This is what makes hard links different from a copy, where editing one leaves the other frozen at its old contents.

ln has two flags you will actually reach for. The first cures the silence. -v stands for verbose, and it makes ln announce what it did instead of saying nothing.

The second is -f, for force. Normally, if the new name already exists, ln refuses and errors out rather than clobber something. -f tells it to replace the existing name. Use it when you mean to overwrite.

Run a verbose link so you can see ln speak for once:

ln -v original.txt copy2.txt

prompt: student@linuxcamp:~$ answer: ln -v original.txt copy2.txt output: 'copy2.txt' => 'original.txt' hint: Add the -v flag before the two names: ln -v original.txt copy2.txt

With -v, ln printed 'copy2.txt' => 'original.txt', its way of saying the new name points at the original. The arrow reads new-name to target. That is the same silent action from before, now narrated. Reach for -v when you are linking several files and want confirmation of each one, and reach for -f when a name is already taken and you deliberately want to replace it.

Hard links have two hard limits, and both produce errors you should recognise on sight.

First, a hard link cannot point at a directory (a folder). The system forbids it, because directory hard links could tangle the filesystem into loops it cannot escape. Try it and you get told no:

ln somedir linkdir

prompt: student@linuxcamp:~$ answer: ln somedir linkdir output: ln: somedir: hard link not allowed for directory hint: Point ln at a folder to see it refuse: ln somedir linkdir

The message ln: somedir: hard link not allowed for directory is the system protecting the filesystem from loops. This is exactly the gap that the symbolic link fills: ln -s CAN point at a directory, which is one big reason it exists. That is your next lesson. Second, a hard link cannot cross from one filesystem to another, because an inode number only means something on its own disk. Try to link across disks and you get ln: failed to create hard link: Invalid cross-device link. For both jobs, linking a folder or reaching another disk, the tool is ln -s, coming up next.

Scaffolding off. No command is printed this time. You have every piece you need.

You have a file called report.txt. You need a second name for it, backup.txt, that points at the very same data, no copy, no extra disk space. Create that hard link now.

prompt: student@linuxcamp:~$ answer: ln report.txt backup.txt output: hint: Same shape as the very first drill: ln, the existing file, then the new name. ln report.txt backup.txt

Silence again, which means success. backup.txt now points at the same inode as report.txt, one file under two names. If you wanted to prove it, ls -i report.txt backup.txt would show the same inode number on both lines, and ls -l would show the link count sitting at 2. You recalled the ln <existing> <new> shape from memory, which is the exact move the real machine will ask of you.

Because ln is silent when it works, its errors are how it talks to you. Here are the three you are most likely to meet, each with what it means and what to check first:

The message ln: failed to create hard link 'hardlink.txt' => 'original.txt': File exists means the new name is already taken. ln will not overwrite it by default. Check first: did you already make this link, or does a real file own that name? If you truly mean to replace it, add -f to force it.

The message ln: somedir: hard link not allowed for directory means you pointed ln at a folder. Hard links cannot name a directory. Check first: is your target a folder? If you need to link a folder, this lesson's tool is the wrong one; use ln -s, coming next.

The message ln: failed to create hard link: Invalid cross-device link means the two names would sit on different filesystems, and an inode number only means something on one disk. Check first: are the source and destination on the same drive? If they are on different disks, again the tool is ln -s.

Every one of these errors, File exists, not allowed for directory, and Invalid cross-device link, has the same escape hatch for the last two: the symbolic link, ln -s. It can cross disks and it can point at folders. That is why it is the lesson right after this one. The plain hard link you learned here is the tight, same-disk, same-inode tool; -s is the flexible one.

You earned this cheat sheet. Every row is something you just ran or saw:

The one idea under all of it: a hard link is not a copy and not a shortcut. It is a second name for the same inode, the same data, the same file. ls -i is how you prove two names are one file, and the link count in ls -l is how you count the names.

Remember the two limits, because they decide which tool you reach for. A hard link cannot name a directory and cannot cross to another disk. When you hit either wall, the answer is the symbolic link, ln -s, your very next lesson.

The practice terminal has shown you the whole of the hard link. ln gives a file a second name and says nothing when it works, so you prove the result yourself. ls -i shows the shared inode, ls -l shows the link count, and writing through one name and reading through the other proves it really is one file. You met -v and -f, and you saw the two walls a hard link cannot cross. Every one of those you typed yourself.

The Manipulation module ends with one real Linux machine, the module capstone mission, and that is where you put these pieces to work for real. A VM boots just for you, with a live shell and real files on a real disk. It hands you objectives that use exactly what you practiced across this module: creating, copying, moving, and now linking files, and proving each change on disk. One difference from here: the mission shows no commands. You read the objective, you recall the command, you type it. That recall is what makes it stick.

Finish the other Manipulation lessons, then go give a real file a second name.

Practice ln in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.