Getting started
{{ .windows }} On Windows you start WSL by searching for "Ubuntu" in the Windows Start Menu. You should see something like this:
{{ .macos }} On macOS, press !kbd[!cmd+Space] and then enter "terminal" to open a terminal. You should see something like this:
{{ .linux }}
On Linux, how you open a terminal depends on your distribution. Usually one of
!kbd[Ctrl+Alt+t], !kbd[!win+t] or !kbd[!win+Enter] opens one. Otherwise, search
for "how to open terminal <insert your distro here>
". You should see something
like this:
You can also open a terminal in VSCode if you prefer working there. Open the
command palette (!kbd[!ctrl+Shift+p]) and type "terminal: create new terminal"
and press !kbd[Enter]. This should open a new terminal at the bottom of your
screen. You can also open one by clicking on Terminal
in the menu bar at the
top of your screen, and then pressing New Terminal
.
With a terminal open, what are we looking at? Exactly what you see will depend
on which terminal you are using, if yours looks different that is okay. At the
start of the line of text you will see your username followed by @
, and then
the name of your computer. After that you will see the current directory. Your
terminal keeps track of something called its working directory, which is
where on your file system it is located at the moment. Right now it is ~
,
which is a symbol used to represent your home directory. Every user has a
home directory where all of their files are stored. At the end of the line you
will see a solid block, which is your cursor. Here we can write commands and
run them by pressing !kbd[Enter].
We can print the exact location of our working directory by running the command
pwd
(print working directory). This should print /home/
followed by your username. You can also list the contents of your working
directory with the command ls
(list).
Commands are case sensitive. This means that
pwd
andPWD
aren't the same thing, and you will get error if you try and run the second one.
Creating and deleting files and directories
We can create directories with the command mkdir <name>
(make
directory). The text inside angle brackets is a placeholder which shows
that a command needs some extra data to run properly. This extra data is called
an argument and you are supposed to replace them (including the <>
) with what
you want before running the command. For commands that require multiple
arguments, you separate them with spaces. In the case of mkdir
, we replace it
with the name of the directory we want to create. Try running mkdir test_directory
and look at the changes with ls
.
To create files, you can use touch <path>
. Here we replace <path>
with the
name of the file we want to create. Run touch file1
and touch file2
. If you
run ls
now you should see the two files. We can create multiple files at once
by giving multiple names as arguments. Try this with touch file3 file4
.
To remove files, use rm <path>
, where <path>
is the name of the file we
want to remove. Here we can also list multiple files we want to delete.
[Danger] Be careful with using
rm
as the files will not be moved to the trashcan like they would if you deleted them from a user interface. If you userm
, the files are gone and you cannot recover them. You won't be able to remove system critical files without extra authentication, but you should still be careful. The next chapter covers Git, which can protect you from costly mistakes like this!
If you want to open up VSCode to start editing some of the files, you can run
code
to open up the working directory in the editor. You can also use code <path>
to open a specific file or directory. If you are using a different
editor, there is probably a command to start it from the terminal too.
{{ .window}} On windows the ability to do this requires you to install VSCode to your path. This is one of the options that should be pre-selected when you install it, and this will not function if you unchecked that box.
Navigation
Up until now we have only run commands from our home directory, but it would be
cumbersome to do everything from here. We can change our working directory with
cd <path>
(change directory). Here we replace <path>
with the name
of the directory we want to move into. Try running cd test_directory
. You can
verify that you have changed the working directory by running pwd
again.
You can also move to the previous directory using cd ..
. The argument we are
giving to cd
is more generally known as a path, which we will cover in more
depth in the next subchapter.
In many terminals you can autocomplete commands and arguments by pressing !kbd[Tab]. If there are multiple alternatives, you will get a list of them. You might need to double-tap !kbd[Tab] to see all available alternatives.
You might wonder how you can refer to a directory with a space in its name, as
we use spaces for separating arguments. In these cases, we can surround the
path in quotation marks like this cd "My directory"
. This applies to any
arguments that contain spaces, not just directory names. When using tab
completion, you might instead get a suggestion of cd My\ directory
. Here
backslash acts as a way to "escape" the following space.
You can access your command history using the up and down arrow keys.
It is common to alternate between cd
and ls
when navigating in the
terminal, but this can quickly clutter up the screen. You can clear it by
either running the command clear
, or by pressing
!kbd[Ctrl+l].
Special shortcuts
There are a couple of shortcuts that might be helpful.
- !kbd[Ctrl+c] terminates the current program (does not force a program to exit if it is busy).
- !kbd[Ctrl+z] sends the program to the background, can be sent back to the
foreground by running
fg
. - !kbd[Ctrl+d] sends an End of Line (EOL) to the terminal. This usually exits the current program if it is interactive.