LearnLinux FoundationsNavigation

The cd Command

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

Stop looking and start moving. Walk into directories by name with cd, jump home with a bare cd, and toggle back to where you just were with cd -. Confirm every landing with pwd, right in the practice terminal on the recon-base filesystem.

Back on recon-base, the same box you mapped with ls and walked through in the paths lesson. There you moved with cd by spelling out addresses: the full absolute path from the root, or a relative path from where you stood.

Run ls and you can see a folder called documents sitting right in front of you. You could reach it by typing out its whole address. But watch a working engineer for five minutes and you will notice something: they almost never type long addresses. They hop between folders in two or three keystrokes. So here is the question this lesson answers: what are the short moves that make cd feel effortless?

The black boxes below are a practice terminal: a safe sandbox that only checks the one command each step teaches, so you cannot break anything. The real machine, where you run the full mission, is the lab at the very end of this module.

A directory is just Linux's word for a folder; the two mean the same thing. Take one last look from where you stand. The ~ is a shortcut meaning "my home folder," which on this machine is /home/student. List what is inside recon-base:

ls ~/recon-base

prompt: student@linuxcamp:~$ answer: ls ~/recon-base output: broken-link comms documents intel latest-briefing logs ops hint: ls lists names. Point it at recon-base: ls ~/recon-base

There they are, seven names, documents among them. You are still standing outside, at your home folder. You could walk in the long way, with a full address. By the end of this lesson you will do it in fewer keystrokes, and bounce between two folders without typing a path at all.

Remember from the paths lesson: the command that moves you between directories is cd, short for "change directory." You point it at a destination and your terminal walks there. After that, everything you type runs from the new spot.

Think of your terminal as always standing inside exactly one folder. ls lets you look around that folder. cd picks you up and sets you down in a different one.

Walk into recon-base. Type this exact command and press Enter:

cd ~/recon-base

prompt: student@linuxcamp:~$ answer: cd ~/recon-base output: hint: Type cd, a space, then the folder to enter: cd ~/recon-base

Nothing printed. That blank line is not a mistake; it is cd quietly telling you it worked. On Linux, silence means success. cd never announces where it took you. It just takes you there, and the prompt changes to show your new spot.

Since cd says nothing, you confirm the move a different way. You met this in the paths lesson too: the command pwd, short for "print working directory," prints the full address of the folder you are in right now. Your working directory is simply that folder, the one your terminal is sitting in.

You just ran cd ~/recon-base. Ask pwd to prove where you landed:

pwd

prompt: student@linuxcamp:~/recon-base$ answer: pwd output: /home/student/recon-base hint: Just three letters, no path. Type: pwd

There is your full address: /home/student/recon-base. You are no longer standing outside at your home folder; you are inside recon-base. Notice the prompt changed too, from ~ to ~/recon-base, showing the same move in shorthand. cd walked you in, and pwd confirmed it.

You do not have to spell out a long address every time. When the folder you want is sitting right inside the one you are already in, you can name just that folder and step straight into it.

You are standing in recon-base, and documents is right here inside it, the very folder you could see but not open at the start. Step into it by name:

cd documents

prompt: student@linuxcamp:~/recon-base$ answer: cd documents output: hint: The folder is right inside you. Name it: cd documents

Silent success again. You named a folder that was sitting right inside your current one, and cd stepped you into it. Confirm it the way you always do:

Ask pwd where naming documents dropped you.

pwd

prompt: student@linuxcamp:~/recon-base/documents$ answer: pwd output: /home/student/recon-base/documents hint: The same three letters as before: pwd

/home/student/recon-base/documents. You are now two folders deep, one step below where you started this drill. Naming a folder that lives right inside you is the fastest way to move down. There is a matching way to move back UP, cd .., which uses the two dots you will meet in a later lesson; for now, just know it waits for you.

cd moves you; pwd tells you where you are. They are partners. Whenever you lose track of where a series of moves left you, pwd gives you the answer in one word.

You are deep inside documents. Suppose you want to get all the way back to your home folder, /home/student. You could type the whole address. There is a much faster way: type cd by itself, with nothing after it.

cd with no destination always means one thing: go home. Try it:

cd

prompt: student@linuxcamp:~/recon-base/documents$ answer: cd output: hint: No path at all. Just the two letters and Enter: cd

Silent, like every successful cd. But look at the prompt: it snapped back to a bare ~, the shorthand for your home folder. A bare cd is the shortest trip home there is, and it works from anywhere on the machine, no matter how deep you have wandered.

Prove the bare cd really took you home. Ask pwd:

pwd

prompt: student@linuxcamp:~$ answer: pwd output: /home/student hint: One more pwd to read the full address: pwd

/home/student, your home folder, exactly where a bare cd promised to put you. There is a second way to say the same thing: cd ~, the jump-home move you ran in the paths lesson. The ~ is that home shortcut, so cd ~ and a bare cd land in the identical place. Most people type the bare cd because it is fewer keystrokes.

One more move and you have the everyday set. Often you bounce between two folders, running something here, checking something there. Retyping long addresses gets old fast. cd - (cd, a space, then a dash) is the shortcut: it jumps you straight back to the directory you were in just before your last move.

Right now you are home, and the place you were just before this was documents. So cd - should throw you back there. This one is special: unlike every other cd, it is not silent. Run it and watch:

cd -

prompt: student@linuxcamp:~$ answer: cd - output: /home/student/recon-base/documents hint: cd, a space, then a dash. Type: cd -

This time something printed. cd - is the one form of cd that speaks: it echoes the full address of the directory it just jumped you into, here /home/student/recon-base/documents. That is why you did not need a pwd afterward; cd - already told you where you landed. Run cd - again and it would toggle you right back home. It flips between your two most recent spots, like an undo button for your last move.

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

You are home at /home/student. Walk straight into recon-base in one move, using the folder's name from home.

prompt: student@linuxcamp:~$ answer: cd recon-base ||| cd ~/recon-base output: hint: The folder recon-base sits right inside your home folder. Name it after cd, the way you stepped into documents.

In you go, silent as ever. From your home folder, recon-base was sitting right there inside you, so naming it was all it took. Now confirm and finish the loop.

Still no command shown. Ask the machine for the full address of where that walk left you.

prompt: student@linuxcamp:~/recon-base$ answer: pwd output: /home/student/recon-base hint: The one word that prints where you are standing.

/home/student/recon-base. You walked in from memory and confirmed it from memory, which is exactly the move the real machine will ask of you. No paths on screen, just you and the command.

You earned this. Every row is something you just ran on recon-base:

The one habit worth burning in: cd moves you and stays silent, so you confirm with pwd. The single exception is cd -, which prints the address it sends you to. Everything else about moving around, the full addresses that work from anywhere, and the two dots for climbing back up, is its own short lesson nearby.

Lost after a few moves? Do not guess. Type pwd and read your exact address, then decide your next cd from there.

The sandbox checked one command at a time. The real machine will not hold your hand.

You will put all of this together in the Navigation capstone mission, "Recon Base: Navigation Sweep," the one lab at the end of this module. There a live Linux VM boots just for you on the same recon-base filesystem. You move through it for real: stepping into folder after folder with the cd moves you have been drilling and confirming every landing with pwd. No commands are shown there. You read the objective, you recall the move, you type it. That recall is what makes it stick.

Finish the other Navigation lessons, then go run the sweep for real.

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