Learn › Linux Foundations › Manipulation
realpath - a hands-on Linux lab on a real virtual machine.
Resolve any path to its one true absolute, canonical form with realpath. It makes paths absolute, collapses . and .., and follows symlinks to their real target. Learn -s to stop at a symlink, -e and -m to set the exist-or-not rule, and --relative-to to answer from another folder, plus the No such file or directory error you will meet.
Type cd docs and you land somewhere. Type cd ../student/./docs and you land in the exact same folder. Same place, two names. In fact one folder can be pointed at by a hundred different paths, and shortcuts called symlinks can add even more.
So which name is the REAL one? When a script logs where it worked, or a program stores where a file lives, it cannot keep a hundred spellings. It needs the one true address. There is a command whose only job is to take any path you hand it, however messy, and print back that single canonical answer. It is called realpath.
The black boxes below are a practice terminal: a safe sandbox that checks the one command each step teaches, so you cannot break anything. Your home folder here is /home/student, and it already holds a file notes.txt, a folder docs, and a shortcut named shortcut that points at docs. The outputs shown are exactly what you will see against that layout.
realpath takes a path and resolves it to its canonical form. Canonical just means the one official version, with all the ambiguity removed. It does three cleanups in one pass.
First, it makes the path absolute: it always starts from the root /, no matter where you are standing. Second, it expands the shorthand pieces: . means here and .. means the folder above, and realpath replaces them with real folder names. Third, it follows every symlink, a symlink being a signpost file that points at another location, so you get the true target, not the signpost.
Start with the plainest case. You are in /home/student, and you ask for the full address of the file notes.txt sitting right next to you:
realpath notes.txt
prompt: student@linuxcamp:~$ answer: realpath notes.txt output: /home/student/notes.txt hint: Type realpath, a space, then the filename: realpath notes.txt
You typed a bare filename and got back a complete address that starts at the root /. realpath saw you were standing in /home/student, glued that in front of notes.txt, and handed you /home/student/notes.txt. That is the whole idea in miniature: a short relative name in, the one true absolute path out. Nothing on disk changed. realpath only reads and reports; it never moves or renames a thing.
realpath is old. The idea comes from a C library function of the same name that has resolved paths since early Unix. The command-line tool ships today as part of GNU coreutils on nearly every Linux machine, so the muscle you build here works almost everywhere you will ever log in.
The plain case was easy because the path was already tidy. The real value shows up when the path is a mess. realpath untangles . (here) and .. (up one level) and collapses the detours into the straight-line answer.
Before you run this, read the path and predict where it lands. From /home/student, the path ../student/./docs means: go up to /home, come back down into student, stay put with ., then step into docs. A silly walk that ends exactly at the docs folder. Now watch realpath cut through it:
realpath ../student/./docs
prompt: student@linuxcamp:~$ answer: realpath ../student/./docs output: /home/student/docs hint: Hand realpath the whole messy path exactly as written: realpath ../student/./docs
The winding walk and its clean answer point at the same folder, and realpath printed only the clean one: /home/student/docs. It cancelled the .. against the home step, dropped the do-nothing ., and gave you the direct address with no detours left in it. This is why scripts lean on realpath. A path assembled from pieces is often full of . and .., and one call turns it into something you can safely store, compare, or log.
Now the third cleanup, and the one people find most useful. A symlink is a small file that holds nothing but a pointer to somewhere else, like a signpost that reads go over there. Your sandbox has one named shortcut that points at the docs folder.
When you hand realpath a symlink, it does not stop at the signpost. It walks through to whatever the signpost points at and prints THAT true location. Ask it to resolve shortcut and see where the signpost really leads:
realpath shortcut
prompt: student@linuxcamp:~$ answer: realpath shortcut output: /home/student/docs hint: Give realpath the name of the shortcut: realpath shortcut
You asked about shortcut, but the answer says /home/student/docs. realpath followed the signpost through to its real target and reported the destination, not the pointer. That is the default behavior: symlinks get followed all the way. It matters because two different names, shortcut and docs, just resolved to the exact same true path, which is precisely how a script can tell that two paths are secretly the same place.
Sometimes you do NOT want the signpost followed. You want to normalise the text of the path, tidy up its . and .., but leave a symlink pointing where it points. The -s flag does exactly that. Think of -s as short for stay: normalise the spelling, but do not walk through symlinks.
Run the same shortcut from the last step, but this time with -s, and watch the answer change:
realpath -s shortcut
prompt: student@linuxcamp:~$ answer: realpath -s shortcut output: /home/student/shortcut hint: Add -s between realpath and the name: realpath -s shortcut
Without -s, realpath shortcut gave /home/student/docs, the real target. With -s, it gives /home/student/shortcut, the signpost itself made absolute but not followed. Same command, one flag, opposite intent. Default realpath answers where does this really lead; realpath -s answers what is the tidy absolute spelling of this exact name. Reach for -s when you care about the name you were handed, not the place it eventually points to.
By default realpath is relaxed. It will happily resolve a path even if the last piece does not exist yet, which is handy when you are building a name for a file you are about to create. Two flags let you set the rule you want.
-e means every component must exist. If any part of the path is missing, realpath refuses and prints an error instead of a made-up answer. -m is the opposite: allow missing components, resolve the path as text no matter what, never complain. Try -e on a name that is really there, notes.txt, so it passes cleanly:
realpath -e notes.txt
prompt: student@linuxcamp:~$ answer: realpath -e notes.txt output: /home/student/notes.txt hint: Put -e between realpath and the filename: realpath -e notes.txt
Because notes.txt really exists, -e was satisfied and you got the normal absolute path, /home/student/notes.txt. The difference from plain realpath is invisible here because the file is present. Point -e at something that is NOT there and it fails loudly instead, which is exactly what you want in a script that must not proceed on a missing file. The next step shows that failure on purpose.
Time to see realpath fail, because knowing its one common error means you will never be stumped by it. By default, if you ask realpath to resolve a name that does not exist, it stops and tells you so. There is no file called ghost.txt in your sandbox. Ask for it anyway:
realpath ghost.txt
prompt: student@linuxcamp:~$ answer: realpath ghost.txt output: realpath: ghost.txt: No such file or directory hint: Just ask realpath for a file that is not there: realpath ghost.txt
There it is: realpath: ghost.txt: No such file or directory. It reads left to right as who complained (realpath), what it choked on (ghost.txt), and why (No such file or directory). The first thing to check is your spelling and where you are standing, since a relative name is resolved from your current folder. If you actually MEANT to build a path for a file that does not exist yet, add -m and realpath will resolve the text and stay silent about the missing piece.
One more form worth knowing. Usually you want the full absolute path, but sometimes you want the answer measured FROM a particular folder instead. --relative-to=DIR prints the path to your target as seen from DIR, using .. steps if it has to climb.
From /home/student, ask for docs but expressed relative to the /tmp folder. To get from /tmp to /home/student/docs you must climb up and over, so expect some .. in the answer:
realpath --relative-to=/tmp docs
prompt: student@linuxcamp:~$ answer: realpath --relative-to=/tmp docs output: ../home/student/docs hint: Two dashes, relative-to, an equals sign, the folder, then your target: realpath --relative-to=/tmp docs
The answer ../home/student/docs reads as a route: from /tmp, step up once with .. to reach /, then walk down into home, student, docs. realpath still resolved everything to the truth first, then rephrased that truth as directions starting from the folder you named. This is how tools build short, portable paths between two known locations without hard-coding a long absolute string.
Scaffolding off. No command is printed this time. You have every piece you need.
You are standing in /home/student. There is a symlink named shortcut in front of you and you do not trust it. Print the ONE true absolute path it really points at, following the signpost all the way through, so its real destination shows on screen.
prompt: student@linuxcamp:~$ answer: realpath shortcut output: /home/student/docs hint: This is the plain, default form of the command from earlier: realpath, then the name of the signpost.
You recalled it cold: plain realpath shortcut, no flags, and it followed the signpost through to /home/student/docs. Default realpath always resolves symlinks, so the bare form was exactly right. You did not need -s (that would have stopped at the signpost) and you did not need -e (the target is really there). Picking the plainest form that answers the question is the mark of someone who understands the tool, not just its flags.
Two other messages you might bump into, with what each means and what to check first.
The message realpath: missing operand means you pressed Enter with nothing after the command. realpath needs at least one path to resolve. Check that you actually typed a name after it, such as realpath notes.txt.
The message realpath: --relative-to: option requires an argument means you wrote --relative-to but forgot the folder after the equals sign. Check the shape reads --relative-to=/some/dir, with the directory attached by = and no space.
You may have met readlink -f, which resolves a path much the same way. realpath is the dedicated tool for this job and carries the extra flags you just learned, like -s, -e, -m, and --relative-to. When you specifically want the one true path with control over how it resolves, reach for realpath.
You earned this cheat sheet. Every row is a form of realpath you just ran:
The one idea under all of it: realpath turns any path, however messy or indirect, into the single canonical absolute address that names one real place on disk.
The everyday reach is plain realpath name: it makes a path absolute, tidies its . and .., and follows its symlinks in one shot. Add a flag only when you want to change that default, -s to keep a symlink unfollowed, -e to insist the path exists, -m to allow it not to.
The practice terminal has shown you the whole of realpath. It resolves any path to its one true absolute form, collapsing . and .. and following symlinks, with -s to stop at a signpost, -e and -m to set the exist-or-not rule, and --relative-to to answer from another folder. 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 a real filesystem full of files, folders, and symlinks. It hands you objectives that use exactly what you practiced across this module. 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 resolve real paths on a real machine.
Practice realpath in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.