CodeStride
The Command Line

Moving around folders in Git & Command Line

Topic 2 of 32 · 8 minFree lesson

Folders hold files and other folders, like boxes inside boxes. In the terminal you don't see them on screen, so you move between them with commands. This topic shows you how to get around and how to always know where you are.

Seeing what's here

ls lists what's inside the folder you're in. You'll learn its options in the next topic. For now, it shows you where you can go:

Example 1 · Terminal
$ lsnotes photos projects recipes

Your home folder has four folders in it: notes, photos, projects and recipes.

Going into a folder

cd means "change directory". Give it the name of a folder, and the terminal moves into it. cd prints nothing when it works, so check with pwd:

Example 2 · Terminal
$ cd notes$ pwd/home/learner/notes$ lsideas.txt shopping.txt todo.txt

Now ls lists the files in notes, because that's where the terminal is.

Going back up

Two dots, .., always mean "the folder above this one", also called the parent folder:

Example 3 · Terminal
$ cd ..$ pwd/home/learner

You can go several folders at once by putting names together with / between them. projects/website means "the website folder inside projects":

Example 4 · Terminal
$ cd projects/website$ pwd/home/learner/projects/website$ cd ../..$ pwd/home/learner

../.. goes up two folders, one for each ...

Paths

A path says where a file or folder is. There are two kinds:

  • A relative path starts from the folder you're in, like notes or ../recipes.
  • An absolute path starts with / and goes from the very top of the computer, like /home/learner/recipes. It works the same from anywhere.
Example 5 · Terminal
$ cd /home/learner/recipes$ pwd/home/learner/recipes$ cd ../photos$ pwd/home/learner/photos

The first cd uses an absolute path. The second goes up from recipes and into photos, which sit side by side.

Going home

~ is short for your home folder, and cd with nothing after it takes you there too. cd - jumps back to the folder you were in before, and prints its path:

Example 6 · Terminal
$ cd ~/projects$ pwd/home/learner/projects$ cd$ pwd/home/learner$ cd -/home/learner/projects
Common mistakecd only goes into folders. If you give it a file, or a folder that doesn't exist (often a typo), it says so and stays where it was.
Example 7 · Terminal
$ cd ~$ cd recipebash: cd: recipe: No such file or directory$ cd notes/todo.txtbash: cd: notes/todo.txt: Not a directory$ pwd/home/learner
TipLost? pwd tells you where you are, and cd on its own always brings you home.

Try it yourself

Run the code in the editor and answer 8 practice questions on moving around folders. It's free, you only need an account.