LearnLinux FoundationsManipulation

ln -s

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

Plant a signpost with ln -s: a symbolic link that stores a PATH to another file, unlike a hard link that shares the inode. The command is silent, so you prove it with the ls -l arrow and read through it with cat. Replace links with -f, spot a dangling link by its No such file or directory error, and know why a symlink can point at a directory and cross disks when a hard link cannot.

Picture a folder with two names in it: original.txt and shortcut. You open shortcut and you see the exact contents of original.txt. You edit through shortcut and original.txt changes too. Yet shortcut holds none of that text itself. It is almost empty.

So what IS shortcut? It is a symbolic link: a tiny file whose whole job is to point at another file by name. It is a signpost, not a copy. This lesson is about the one command that plants that signpost, ln -s.

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 -s prints nothing when it works, so every drill runs it and then proves what changed on disk with a second command like ls -l or cat. All the files and paths here are set up for you, so the outputs are exactly what you will see.

ln is short for link. On its own it makes a hard link, a second name that shares the very same file. Add the -s flag and you get something different: a symbolic link, also called a soft link or symlink.

A symlink does not share the file. It is its own small file that stores one thing: the path to the target. Reading the link means following that stored path to whatever it points at. If the target is a recipe card, a hard link is a second identical card, but a symlink is a sticky note that just says go read the card in the top drawer.

The shape is always the same: ln -s TARGET LINKNAME. The target comes first (what you are pointing at), the link name comes second (the signpost you are creating). Most engineers get the order backwards at least once, so say it out loud: target first, then the new name.

Symbolic links arrived in Berkeley Unix (4.2BSD) in 1983, a decade after hard links. Hard links could never point at a folder or reach across to another disk. The symlink was invented to lift both limits, and it is why a symlink can point at a directory and cross from one filesystem to another, which a hard link still cannot do.

Time to make one. In the folder there is already a real file, original.txt. You will create a symlink named shortcut that points at it. Give the target with its full path so the signpost is unambiguous.

Because ln -s succeeds silently, you will run it and see nothing, then use ls -l to prove the link exists and see where it points. Before you run it, decide what you expect: the command line itself will print nothing at all.

ln -s /home/student/original.txt shortcut

prompt: student@linuxcamp:~$ answer: ln -s /home/student/original.txt shortcut output: hint: Flag first, then target, then the new name: ln -s /home/student/original.txt shortcut

Nothing printed, and that is success. ln -s never announces itself; a silent return means the link was made. The command did not copy any text. It created one small file named shortcut whose only content is the path /home/student/original.txt. Next you make that invisible result visible.

A silent command needs proof. The proof for a symlink is the arrow that ls -l draws. ls -l prints one long line per file, and for a symlink it adds -> followed by the path the link stores. That arrow is how you read a signpost from the outside.

Look at the link you just made:

ls -l shortcut

prompt: student@linuxcamp:~$ answer: ls -l shortcut output: lrwxrwxrwx 1 student student 26 Jul 5 12:00 shortcut -> /home/student/original.txt hint: List the one file in long form: ls -l shortcut

Two things in that line tell the whole story. The line starts with the letter l, not the - a normal file gets: l is for link, so ls is telling you this is a symlink at a glance. Then, at the end, shortcut -> /home/student/original.txt. The arrow points from the signpost to its target. That path is literally what the file stores. You are reading the signpost's text.

A signpost is only useful if following it works. When you read a symlink, the system follows the stored path for you and hands back the target's contents, as if the link were the real file. The lesson file original.txt holds one line of text. Read it through the link and watch that line appear.

cat shortcut

prompt: student@linuxcamp:~$ answer: cat shortcut output: the original file hint: cat follows the link to its target: cat shortcut

You ran cat on shortcut, but the text you saw, the original file, lives in original.txt. The link held no text of its own; the system read the stored path, followed it to the target, and printed the target's line. That is the entire point of a symlink: use the signpost, land on the real file. Edit through shortcut and you would be editing original.txt, because there is only one real file here.

Here is the catch every engineer meets. A symlink stores a path, not the file itself. Delete the target and the signpost still stands, but now it points at nothing. That is a dangling link (also called a broken link). The link file is perfectly real; following it is what fails.

In the folder there is a link named brokenlink whose target was already deleted for you. First see that the link itself still exists with ls -l:

ls -l brokenlink

prompt: student@linuxcamp:~$ answer: ls -l brokenlink output: lrwxrwxrwx 1 student student 22 Jul 5 12:00 brokenlink -> /home/student/gone.txt hint: List it in long form like before: ls -l brokenlink

The link is right there, arrow and all: brokenlink -> /home/student/gone.txt. The signpost is intact. But /home/student/gone.txt no longer exists. So the arrow points into empty space. Now watch what happens the moment you try to FOLLOW that signpost.

Try to read through the broken link. The system follows the stored path, finds no file waiting there, and reports exactly that. This is one of the two error messages you have to know on sight.

cat brokenlink

prompt: student@linuxcamp:~$ answer: cat brokenlink output: cat: brokenlink: No such file or directory hint: Try to read through the broken link: cat brokenlink

cat: brokenlink: No such file or directory is confusing the first time, because brokenlink clearly DOES exist, ls -l just showed it. What is missing is its TARGET. The message means the path stored inside the link leads nowhere. First thing to check: run ls -l brokenlink, read the path after the arrow, and see whether that target file is really there. Nine times out of ten the target was moved or deleted.

Try to create a link with a name that is already taken and ln refuses, so it never clobbers something by accident. That refusal is the second error you must know:

ln: failed to create symbolic link 'shortcut': File exists

It means a file named shortcut is already there and ln will not overwrite it blindly. The fix is the -f flag, for force: it removes the existing link first, then makes the new one. Repoint shortcut at a different file, notes.txt, using -f:

ln -sf /home/student/notes.txt shortcut

prompt: student@linuxcamp:~$ answer: ln -sf /home/student/notes.txt shortcut output: hint: Add f to the flags so it replaces the old link: ln -sf /home/student/notes.txt shortcut

Silent again, so it worked. You bundled the flags as -sf, which is just -s and -f together. Without -f this would have stopped with ln: failed to create symbolic link 'shortcut': File exists, because the old shortcut was in the way. With -f, ln quietly deleted the old signpost and planted a new one aimed at notes.txt. Confirm the new target the way you already know:

One ls -l closes the loop. The same shortcut name now points somewhere new. Read the arrow and confirm it moved from original.txt to notes.txt.

ls -l shortcut

prompt: student@linuxcamp:~$ answer: ls -l shortcut output: lrwxrwxrwx 1 student student 23 Jul 5 12:00 shortcut -> /home/student/notes.txt hint: List it in long form once more: ls -l shortcut

The arrow moved. shortcut -> /home/student/notes.txt proves -f did its job: same link name, brand new target. This is the everyday use of -f, updating a link to point at a newer version of something without a two-step delete-then-recreate dance.

You now know enough to hold both kinds of link in your head. The difference comes down to one idea: a hard link shares the file's inode (its underlying identity number on disk), while a soft link only stores a path to it.

The line that decides most real choices is the last-but-one: delete the target and a hard link still holds the file, but a soft link is left pointing at nothing. Soft links are more flexible (folders, other disks); hard links are sturdier (they cannot dangle). You reach for ln -s far more often, which is why it earns its own lesson.

Prefer ABSOLUTE targets, the ones that start with /, like /home/student/original.txt. A symlink stores its target text exactly as you typed it. Give it a relative target like original.txt and the link only works while you stand in the right folder; move the link elsewhere and it dangles. An absolute path keeps the signpost pointing at the same real file no matter where the link is moved.

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

There is a real file at /home/student/report.txt. Create a symbolic link named latest that points at it. Use the absolute path for the target so the link keeps working even if it is moved later. Remember the order: the flag, then the target, then the name of the new link.

prompt: student@linuxcamp:~$ answer: ln -s /home/student/report.txt latest output: hint: Same shape you have used all lesson: ln, the -s flag, the absolute target path, then the new link name latest.

Silent, which means the signpost is planted. You recalled the whole shape from memory: ln -s, then the absolute target /home/student/report.txt, then the new name latest. Run ls -l latest and you would see latest -> /home/student/report.txt, and cat latest would read the report through it. That is the core move of this lesson done without a single prompt. The absolute path means latest will still find report.txt even if you move the link into another folder.

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

The one idea under all of it: a symlink is a signpost that stores a PATH, not the file. Point it with ln -s, read the arrow with ls -l, and remember the two failures, File exists when the name is taken, and No such file or directory when the target is gone.

A few more flags worth knowing exist for later. -v (verbose) prints a line naming each link as it is made. -r builds the target as a relative path for you. And -n treats an existing symlink-to-a-directory as a plain file, so you replace the link itself instead of writing inside the folder it points to. You will not need them today, but you will recognise them when they appear.

The practice terminal has shown you the whole of ln -s. You plant a signpost with ln -s TARGET LINK and prove it with the ls -l arrow. You read through it with cat, replace one with -f, and recognise a dangling link by its No such file or directory error. 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. It hands you objectives that use exactly what you practiced across this module: making, moving, copying, and now linking files. 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 plant a signpost on a real machine.

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