Terminal Essentials

Git is a command-line program, so it helps to be comfortable with a handful of terminal commands: finding out where you are, moving between folders, and creating, viewing and deleting files. If you already use a terminal every day, skim this lesson and move on.

The prompt

A terminal shows a prompt — often your user name, computer name and current folder, ending in $ — and waits for a command. You type the command, press Enter, and any output appears below. In this course the prompt is left out; just type the lines shown in the code boxes.

Where am I?

The terminal is always "in" one folder, the working directory. pwd (print working directory) shows it:

$
pwd
Output
/home/sam

A new terminal usually starts in your home folder, which can also be written as ~. (On macOS it's /Users/name, on Linux /home/name, and in Git Bash on Windows /c/Users/name.)

Making and entering folders

$
mkdir recipes
cd recipes
pwd
Output
/home/sam/recipes

mkdir makes a directory; cd changes into it. A few special names help with moving around:

CommandGoes to
cd ..The parent folder (one level up)
cd ~ or just cdYour home folder
cd -The folder you were in before
cd /The very top of the file system

Press Tab while typing a file or folder name and the terminal completes it for you — a huge time-saver.

Creating files

You'll normally create and edit files in your code editor. For quick examples, this course uses echo with redirection: > writes text into a file (replacing what was there), and >> adds it to the end.

$
echo "Pancakes" > pancakes.txt
echo "Serves 4" >> pancakes.txt
cat pancakes.txt
Output
Pancakes
Serves 4

cat prints a file's contents. To create an empty file, use touch:

$
touch notes.txt

Listing files

$
ls
Output
notes.txt  pancakes.txt

Files whose names start with a dot are hidden, and ls skips them. Add -a (all) to see them — you'll need this to spot Git's own .git folder and .gitignore files:

$
touch .hidden-settings
ls -a
Output
.  ..  .hidden-settings  notes.txt  pancakes.txt

. means "this folder" and .. means "the parent folder"; they appear in every directory.

Copying, moving and deleting

$
cp pancakes.txt waffles.txt
mv notes.txt shopping-list.txt
rm .hidden-settings
ls -a
Output
.  ..  pancakes.txt  shopping-list.txt  waffles.txt

cp copies, mv moves or renames, and rm deletes. Be careful with rm: files deleted this way skip the recycle bin. To delete a folder and everything in it, use rm -r foldername.

Paths

A path written from the top of the file system, like /home/sam/recipes/pancakes.txt, is absolute. A path like recipes/pancakes.txt or ../other-project is relative to the folder you're in. Git commands accept both.

If a name contains spaces, wrap it in quotes: cd "my project". It's simpler to avoid spaces in project folder names altogether.

Useful keys

KeyEffect
↑ / ↓Step through previous commands
TabComplete a file name, folder or (with Git's completion installed) a branch name
Ctrl+CCancel the running command
qLeave a pager — Git shows long output such as git log one screen at a time
clearClear the screen

Opening the folder elsewhere

To see the same folder in your file browser, run open . on macOS, explorer . on Windows, or xdg-open . on Linux. With Visual Studio Code installed, code . opens the folder as a project.

Try it yourself

In your home folder, create a folder called practice, move into it, create two files with echo, list them, rename one with mv, and print its contents with cat. Then go back up with cd .. and delete the whole folder with rm -r practice.