Into the Terminal
This lab will explore one of the most important tools we’ll use all year: 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.
👾 💬 FYI
In this lab, we will just use
$
to represent the terminal prompt unless the path to the current directory is important. If you see this in the lab:$ ls some-directory some-file.txt
but your computer shows something like this:
COMPUTER: terminal_lab username$ ls some-directory some-file.txt
don’t worry. As long as you see the
$
at the end, it means you can put in new commands to your Terminal. Some terminals (including recent versions of Mac OS) use%
instead of$
. That’s fine too.
Terminal Adventure Lab
💻 Open a new Terminal window and enter each command with a
$
one by one.
Just be sure you do not type the $
.
$ cd Desktop/making_with_code/pedprog/unit00/lab01
$ poetry shell
$ ls
adventure pyproject.toml returnToShip.py
returnToShip.py
is a runnable Python file (you can tell by the .py
at the end).
pyproject.toml
is a configuration file for this project. You’ll see this in every lab,
and you may ignore it every time.
$ python returnToShip.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.
👾 💬 Your challenge is to see if you can get the treasure, using just the Terminal
Use Terminal to explore the contents of theadventure
directory.
💻 Begin by going into into the
adventure
directory:
$ cd adventure
$ ls
seafloor sinking.txt
sinking.txt
is a text file, so we can read it.
💻 Try using the
cat
command:
$ cat sinking.txt
✅ CHECKPOINT:
Continue exploring into the depths of the sea to find the treasure. Once you find it, return to the
lab_00_terminal_adventure
directory and runreturnToShip.py
to see if you were successful. If you were unable to escape the monster, try again!Before moving on, answer the following prompts in your notebook:
- What are 3 functions of the Terminal?
- Compare navigating the file system via the Terminal v. the Finder.
Terminal Commands
Below are some Terminal commands which might come in handy on your adventure.
Command | What it does |
---|---|
ls |
List what’s in the current directory. |
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 x.py |
Runs the Python program x.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 . |
mv dir1 dir2 |
Moves dir1 to dir2 or renames if dir2 doesn’t exist. |
cp old.txt new.txt |
Copy a file from old.txt to new.txt . |
mkdir bag |
Creates a new directory called bag |
pwd |
Prints the path to where you are in the filesystem |
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! |
More terminal commands
These are just for fun. There’s lots more–ask Chris!
Command | What it does |
---|---|
say hello |
Makes the computer say hello (Mac only) |
`cat sinking.txt | say` |
cal |
Shows you a monthly calendar |
banner hello |
Just try it |