share

The Terminal

Terminal

In the old days of personal computers, there were no mouses or touchscreens. Everything had to be done by typing commands into the computer, which will output the result as text. Fast forward to today, and software developers still use them a lot. There are few reasons for this:

  • Once you master them, you can often get tasks done faster
  • Many software support the terminal as its main interface.

This text-only interface to control computers is often called the terminal. As cumbersome as it may be for beginners, it is very important to master the terminal.

Terminals often go by many names depending on who you are talking to and what computer you use - Command Prompt, CLI, to name a few. For consistency, we will refer to them as the terminal.

WSL

If you are a Mac or a Linux user, terminals come pre-installed on your computer, so you can skip to the next section. If you use Windows, this is where things get interesting.

Windows has its own terminal as well, called PowerShell. However, there are slight differences between PowerShell and Linux/Mac terminals that do not make them identical. This often confuses developers, since when developers say "terminal", they almost always mean "Linux terminal". Many schools and colleges also use the Linux terminal as their default choice when teaching computer science.

So for a long time, Windows users circumvented this problem by installing virtual Linux machines, dual-booting OS'es, and doing other hacky tricks to get Linux running on their computer. Then came the holy blessing, WSL, or Windows Subsystem for Linux.

Since this is not a CS class, we won't get into the exact details of kernel and whatnot. Just know that when you install WSL on your Windows computer, it actually installs a full-fledges Linux computer into Windows. Image two souls called Windows and Linux sharing a single body, that is, the computer.

At this point, Windows users should install WSL on their computer. Installation procedures can be found here. Several varieties of Linux are offered - we recommend Ubuntu 18.04.

Let's Get Going

Whatever computer you may be on, once you open the terminal you will be greeted with a screen with a text like this:


joe@BruinPC:/home/joe $ 

Breaking this down word by word:

  • joe is the username of the current user
  • BruinPC is the name of the computer (usually your name as well)
  • /home/joe is the folder you currently have open. This may be different depending on the configuration
    of your terminal.

Although not seen in this page, on your terminal you should see a blinking cursor, which is where you will begin typing the commands. There is a boatload of commands you can run that come pre-installed on most terminal systems, and it will take days if we tried to cover them all. Since this series is supposed to provide you with a crash, we will only go over select few commands and tricks you need to know to get started.

Terminal commands

cd


joe@BruinPC:/home/joe $ cd

cd stands for "change directory", i.e. move between folders, or directory.

cd is followed by the location and name of the directory you want to move to, like so:


joe@BruinPC:/home/joe $ cd /

'/' is a special directory called the root, which contains every other directories and files in the computer. You can think of it kind of like the C:Drive folder on Windows computers. Type in the above command, and hit enter. This will execute the command, and your screen should look like this now:


joe@BruinPC:/$ 

You can see how we have moved to the root directory now. Root directories on most systems have a folder called usr, so let's try moving into that:


joe@BruinPC:/$ cd usr

Now you should see:


joe@BruinPC:/usr$ 

To move 'up' a directory, in other words to whatever directory that contains the current directory, type in '..':


joe@BruinPC:/usr$ cd ..

This should bring you back to the root:


joe@BruinPC:/$

Similarly, most usr directories have a subdirectory called bin. We can run cd twice to reach it, or do it in a single stroke like this:


joe@BruinPC:/$ cd usr/bin

One last note is the '/' at the beginning of the directory name we specify. If we start with '/', the terminal will start looking for the specified directory starting at the root, while if you omit it (like user instead of /usr) the terminal will look for the directory starting at the current directory. To see this, try running
cd usr. You should see an error like this:


joe@BruinPC:usr/bin$ cd usr
-bash: cd: usr: No such file or directory

This happens because the current directory, bin, has no directory called usr, however, running cd /usr will work. Again, this is because the terminal will start looking in the root directory, and subsequently finds usr contained within.

ls

ls will list all files and directories in the current directory. For instance, if you run ls in the root directory, you should see something like this:


tomokif@TomokiPC:/$ ls
bin  boot  c  d  dev  etc  home  init  lib  lib64  media  mnt  opt  proc  
root  run  sbin  srv  sys  tmp  usr  var

Moving Forward

This is actually it! these two commands are the bare minimum you need to know in order to get around. That being said, if you have time it is highly recommended you look up additional commands online.

Accessing Data on Your Computer

Now that you know how to get around your computer on the terminal, let's try accessing the Desktop. Mac and Linux has slightly different locations for the Desktop, as seen below:


cd /home/[your username]/Desktop cd /Users/[you username]/Desktop

Replace [you username] with your username for the computer, without the brackets.

WSL users are in for another oddity. Since we essentially have a Linux computer running inside a Windows computer, by default, the terminal will look at files that belong to the Linux portion of the computer, not Windows. So to access files on Windows like Desktop and Documents, you will need to do the following instead:


cd c/Users/[you username]/Desktop

Notice the c at the begging. This will put you into Window's C drive, where you have all of your usual files.

To see if this worked, run ls. You should see a list of all files and directories on your Desktop.

Package Manager

Most computers today come with some flavor of a package manager to manage software installed on the machine. Simply put, it is an app store for computer programs, which you can use from the terminal.

WSL and Linux comes pre-installed with a package manager called apt. We will show how to use it in the next section.

Mac surprisingly does not come with one, so you should install it now:

# Install brew package manager
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Note: the above command was taken from ACM hack's tooling series.