Terminal Adventure
Lab setup
First, make sure you have completed the initial setup.
-
Open Terminal. Run the update command to make sure you have the latest code.
$ mwc update -
Move to this lab's directory.
$ cd ~/Desktop/making_with_code/cybersecurity/unit1/lab_terminal
This lab will explore one of the most important tools we'll use in this course: the Terminal. While it may seem complicated at first, it will quickly become your go-to tool for computer science class. The Terminal is what we'll use to navigate our filesystem, run code files, install software, and do all kinds of other tasks.
Terminal Adventure Lab
๐ป
Open a Terminal window and run mwc update. It's a good idea
to run mwc update every time you start
a new lab to make sure you have the latest and greatest.
๐ป
Open a new Terminal window and enter the commands below, one at a time.
(Don't type the $.)
$ cd Desktop/making_with_code/mwc1/unit1/lab_terminal
$ poetry shell
$ ls
adventure
fancy_printing.py
poetry.lock
pyproject.toml
return_to_ship.py
return_to_ship.py is a runnable Python file (you can tell by the .py at the end).
pyproject.toml and poetry.lock are a configuration files. You'll see them in every lab,
and you may ignore them every time.
๐ป
Run return_to_ship.py to see what happens:
$ python return_to_ship.py
Your adventure has only just begun. You are not yet ready to
return to the ship. More secrets await you in the ocean's depths.
Use `ls` to look around, and use `cd adventure` to start the
adventure...
Your challenge is to see if you can find the treasure and bring it back to the ship, using just the Terminal.
๐ป
Begin by going into into the adventure directory and looking around:
$ cd adventure
$ ls
seafloor sinking.txt
sinking.txt is a text file, so we can read it.
๐ป
Use the cat command to print out the contents of a file:
$ cat sinking.txtTerminal Commands
Here are some Terminal commands which might come in handy on your adventure.
| Command | What it does |
|---|---|
ls | List what's in the current directory. |
pwd | Prints the "present working directory," or the path to where you are in the filesystem |
cd ~ | Go to your home directory |
cd somewhere | Go to somewhere |
cd .. | Go to the parent directory |
open file.txt | Opens file.txt with its default program |
cat file.txt | Prints out the contents of file.txt |
python fun.py | Runs the Python program fun.py |
mv old.txt new.txt | Renames a file from old.txt to new.txt. Also works for directories. |
mv file.txt dir | Moves a file to directory dir. |
cp old.txt new.txt | Copy a file from old.txt to new.txt. |
mkdir bag | Creates a new directory called bag |
rm file.txt | removes (deletes) the file file.txt |
rm -d dir | removes (deletes) the directory dir |
rm -r dir | recursively removes (deletes) the directory dir and all subdirectories and files within that directory. Be careful, this is a powerful tool! |
Extension: SSH
Once you learn how to control your computer with Terminal, it's just as
easy to control other computers in the same way--once you log in. ssh (secure shell)
is a tool for logging into a Terminal session on another computer.
๐ป
Check your username with whoami.
๐ป
Check your computer's name with hostname.
๐ป
Now, switch computers with your partner (or go to another computer in the lab).
Run ssh <username>@<hostname> (replace <username> and <hostname> with yours! The actual command
will look something like ssh chris@chickenpot.local.)
If this is the first time you're logging into this machine, you'll be asked to confirm
that this is the machine you want to log into. Type yes.
$ ssh chris@chickenpot.local
The authenticity of host 'chickenpot.local (X.X.X.X)' can't be established.
ED25519 key fingerprint is SHA256:abcdefg...
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
chris@chickenpot.local$
And now you're in! Navigate around and confirm that you can see all your files.
Try using the wall command to send a message to all the logged-in users on the machine:
wall "Hello out there"
You should see this message appear in the Terminal on your Pi. When you finish, run exit
to log out of the remote session.
SSH is incredibly powerful and useful; many computer scientists use SSH every day. For example:
- Apps and websites are hosted on servers (you'll learn more about servers in the Server Lab). Often developers will ssh into the server to make changes or debug problems.
- Many computer systems are distributed--they run across networks of computers, so you need to use ssh to move between different parts of the system.
- You can ssh into many devices you might not think of as computers, such as cell phones, e-readers, and internet-of-things devices like smart light bulbs, security cameras, and even "smart" wifi-connected appliances like refrigerators and washing machines.