Activity 2: Build a Treasure Hunt
Solo/group: Solo submissions but feel free to work together
Submission: On gradescope ("Lecture 2 Activity")
In Class Activity Part 1: Access/Install Terminal Shell
Directions for MacOS Users and Windows Users.
macOS Users:
Your Mac already has a terminal! Here's how to access it:
-
Open Terminal:
- Press
Cmd + Spaceto open Spotlight - Type "Terminal" and press Enter
- Or: Applications -> Utilities -> Terminal
- Press
-
Check Your Shell:
echo $SHELL # Modern Macs use zsh, older ones use bash -
You are ready. Nothing to install today. Go to Part 2.
Windows Users:
Windows has several terminal options. For this class we strongly recommend/require Git Bash, unless you already are very comfortable using another shell on your machine. (PowerShell aliases some commands to be Linux-like, but they are fairly quirky.)
- Download Git for Windows from git-scm.com
- During installation, take the default options
- Open "Git Bash" from Start menu
Verify Your Setup (Both Platforms)
pwd # Should show your current directory
ls # Should list files
which ls # Should show the path to the ls command
echo "Hello" # Should print Hello
Part 2: Build a Treasure Hunt
You are going to build a small game out of nothing, using only the keyboard. No clicking, no dragging, and no Finder/File Explorer.
At the end you will get to step back and see what you built and... not see what you built?
What you need today - quick reference
pwd # where am I?
ls # what is here?
ls -a # what is here, including hidden things
cd foldername # go into a folder
cd .. # go up one level
mkdir foldername # make a folder
echo "text" > file.txt # write text into a file, replacing whatever was there
echo "text" >> file.txt # add text to the end of a file
cat file.txt # show me the file
nano file.txt # edit a file (ctrl-O saves, ctrl-X quits)
Tab completes a path you have started typing. Up arrow brings back your last command. Use both constantly.
There is a much longer cheat sheet at the bottom but you won't need it for this activity.
Your steps
0. Decide where your course work lives, and make it from the terminal.
You will want one folder to hold everything for this course. Pick now and stick with it. Here's how to do it from the command line.
cd ~ # start at home
mkdir ds210 # make the folder
cd ds210 # go into it
pwd # write this down. This is home base
I (Prof. Wheelock) keep all my code projects in ~/projects, so mine folder is ~/projects/ds210. If you would rather have yours on the Desktop, you can cd ~/Desktop before the mkdir.
Do not make this one by clicking. It's faster now, but once you get good at the command line, you'll work faster than you ever could with point-and-click-ing.
1. Make the hunt, and step inside.
mkdir treasure_hunt
cd treasure_hunt
pwd
That last line should end in /treasure_hunt. If it does not, you are not where you think you are. Sort that out before going on, because everything after this happens here.
2. Sign the guest book.
Make a file called guest_book.txt with your name in it.
echo "Ada Lovelace" > guest_book.txt
cat guest_book.txt
Remember, > means "send this output into that file", creating it if it does not exist. cat shows you it worked.
3. Ask your computer some questions, and keep the answers.
Run each of these. >> adds to the end of the file. > would throw your names away, so the difference matters here.
whoami >> guest_book.txt # your username
hostname >> guest_book.txt # your computer's name
pwd >> guest_book.txt # where you are standing
echo $HOME >> guest_book.txt # your home directory
Now cat the file again. You should see five lines (and wow, you never opened an editor!)
4. Leave a clue.
Make a new file called clue_1.txt containing: The treasure is hidden in plain sight (you have all the tools you need now to figure out how).
5. Build the secret chamber.
mkdir secret_chamber
cd secret_chamber
Inside it, make clue_2.txt containing: Look for a hidden file
6. Hide the treasure. Then watch it vanish.
Make a file called .treasure_map.txt containing: Congratulations. You found the treasure
Note the dot at the front of the name. Now run these two, in this order, and look carefully:
ls # what do you see?
ls -a # now what do you see?
Answer this in writing, appending to the guest book one level up:
echo "A file whose name starts with a dot is ..." >> ../guest_book.txt
.. means the directory above this one, so you are writing into a file you are not standing in.
A hidden file isn't that deep - it's not protected, encrypted, or special. Its name starts with a dot, and ls skips those unless you ask with -a. This is why your folders looks so tidy in Finder or File Explorer and so crowded in the terminal.
7. Stand back and look at what you built.
cd ..
ls -R
That will show you every folder and file you made, all at once. You did all of that without your mouse!
8. Make a zip of it. Change to (cd to) the parent directory of treasure_hunt first. Then, the command is different on each platform:
- Mac:
zip -r treasure_hunt.zip treasure_hunt - Windows, in Git Bash:
tar.exe -a -c -f treasure_hunt.zip treasure_hunt
9. Upload treasure_hunt.zip to Gradescope. Next time we introduce git and GitHub, and we will use that from then on.
10. Optional, for bragging rights. Write a shell script that does all of the above in one go, and upload that too!
Reference: much more than you needed today
Finding the path of something you can already see
Sometimes you will have a folder in Finder or File Explorer and need its path in the terminal. You have a couple options:
1. Drag it into the terminal.
Just drag the folder or file icon into your terminal window annd it will type the path for you. A common use is to first type cd (with a space), then drag a folder from your file browser and hit enter to cd into a folder when you don't know its path.
- Works in Terminal and iTerm2 on a Mac, and in Git Bash on Windows
- Git Bash converts the Windows path into one bash understands, so you get
/c/Users/you/Desktop/ds210and notC:\Users\you\Desktop\ds210 - It is a feature of the terminal window, not the shell, so it may not work in VS Code's built-in terminal or other termminals
2. Ask the file browser.
- Mac: right-click (ctrl-click) the folder/file, hold
Option, and "Copy as Pathname" appears. OrCmd+Ifor Get Info and read "Where" (you can highlight the path there and right click to "Copy as Pathname" as well) - Windows: shift-right-click and "Copy as path". You will get backslashes, so swap them for forward slashes and put
/c/at the front in place ofC:\
Platform-Specific Tips
Mac, Linux, and Windows in Git Bash:
- Your home directory is
~or$HOME. This is the same in Git Bash as it is on a Mac - Hidden files start with a dot (.)
- Try
which commandto find where a command is located - Use
man commandfor detailed help. Git Bash has noman, so usecommand --helpthere instead
Windows outside Git Bash (Command Prompt or PowerShell):
- Your home directory is
%USERPROFILE%(Command Prompt) or$env:USERPROFILE(PowerShell) - Hidden files have the hidden attribute (use
dir /ahto see them) - Use
Get-Help commandin PowerShell orhelp commandin Command Prompt for detailed help - Try
where commandto find where a command is located - A warning: the course notes, the activities, and your TAs all assume Git Bash. We can help you only so far out here. If you are not already comfortable in PowerShell, switching to Git Bash now will save you trouble all semester
Universal Tips:
- Use Tab completion to avoid typing long paths
- Most shells support command history (up arrow or Ctrl+R)
- Combine commands with pipes (
|) to chain operations - Search online for "[command name] [your OS]" for specific examples
Basic Navigation & Listing
# Navigate directories
cd ~ # Go to home directory
cd /path/to/directory # Go to specific directory
pwd # Show current directory
# List files and directories
ls # List files
ls -la # List all files (including hidden) with details
ls -lh # List with human-readable file sizes
ls -t # List sorted by modification time
Finding Files
# Find files by name
find /home -name "*.pdf" # Find all PDF files in /home
find . -type f -name "*.log" # Find log files in current directory
find /usr -type l # Find symbolic links
# Find files by other criteria
find . -type f -size +1M # Find files larger than 1MB
find . -mtime -7 # Find files modified in last 7 days
find . -maxdepth 3 -type d # Find directories up to 3 levels deep
Counting & Statistics
# Count files
find . -name "*.pdf" | wc -l # Count PDF files
ls -1 | wc -l # Count items in current directory
# File and directory sizes
du -sh ~/Documents # Total size of Documents directory
du -h --max-depth=1 /usr | sort -rh # Size of subdirectories, largest first
ls -lah # List files with sizes
Text Processing & Search
# Search within files
grep -r "error" /var/log # Search for "error" recursively
grep -c "hello" file.txt # Count occurrences of "hello"
grep -n "pattern" file.txt # Show line numbers with matches
# Count lines, words, characters
wc -l file.txt # Count lines
wc -w file.txt # Count words
cat file.txt | grep "the" | wc -l # Count lines containing "the"
System Information
# System stats
df -h # Disk space usage
free -h # Memory usage (Linux)
system_profiler SPHardwareDataType # Hardware info (Mac)
uptime # System uptime
who # Currently logged in users
# Process information
ps aux # List all processes
ps aux | grep chrome # Find processes containing "chrome"
ps aux | wc -l # Count total processes
File Permissions & Properties
# File permissions and details
ls -l filename # Detailed file information
stat filename # Comprehensive file statistics
file filename # Determine file type
# Find files by permissions
find . -type f -readable # Find readable files
find . -type f ! -executable # Find non-executable files
Network & Hardware
# Network information
ip addr show # Show network interfaces (Linux)
ifconfig # Network interfaces (Mac/older Linux)
networksetup -listallhardwareports # Network interfaces (Mac)
cat /proc/cpuinfo # CPU information (Linux)
system_profiler SPHardwareDataType # Hardware info (Mac)