Open in VS Code: From your cloned
s26-06643repository, run in the terminal:code sse/01-terminals/01-terminals.ipynb
Module 01: Terminals and Shells (AI-Enhanced)#
Mastering the command line with AI assistance.
Learning Objectives#
Understand terminals, shells, and their role in development
Navigate the file system efficiently
Use AI to explain and generate shell commands
Create executable shell scripts
Customize your shell environment
Why the Terminal Still Matters#
Even with AI coding agents, you need terminal skills because:
AI agents use terminals: Claude Code runs commands in your terminal
Many tools are CLI-first: git, pip, pytest, docker
Automation requires scripting: CI/CD, deployment, data processing
Remote servers: No GUI available
AI Enhancement: Instead of memorizing commands, learn concepts and let AI help with syntax.
Terminal Basics#
What is a Terminal?#
A terminal (or terminal emulator) is a text-based interface to your computer.
What is a Shell?#
A shell is the program that interprets your commands:
Shell |
Description |
|---|---|
|
Bourne Again Shell (default on most Linux) |
|
Z Shell (default on macOS) |
|
Friendly Interactive Shell (modern, user-friendly) |
|
Windows shell (also cross-platform) |
# Check your current shell
echo $SHELL
Exercise 1: Explore Your Shell (5 min)#
Get to know your terminal environment.
Instructions#
Open a terminal and run each of these commands:
Find your shell:
echo $SHELL
Find your Python:
which python(Try
which python3too – are they the same?)Explore your PATH:
echo $PATH | tr ':' '\n'
Questions#
What shell are you using? (
bash,zsh, something else?)How many directories are in your PATH? (Hint: pipe the command above to
wc -l)Where is your Python installed?
AI Assist#
Ask your AI agent: “Explain what the PATH variable does and why it matters.” Read the explanation. Does it match what you see in your own PATH output?
Working with Files#
Viewing File Contents#
# View entire file
cat file.txt
# View with paging
less file.txt # q to quit
# View beginning/end
head -n 10 file.txt
tail -n 10 file.txt
# Follow a file (logs)
tail -f logfile.log
Searching#
# Search in files
grep "pattern" file.txt
grep -r "pattern" . # Recursive
grep -i "pattern" . # Case insensitive
# Find files
find . -name "*.py"
find . -type f -mtime -7 # Modified in last 7 days
AI Help for File Operations#
> How do I find all files larger than 100MB?
Claude: Use find with -size:
find . -type f -size +100M
To see sizes:
find . -type f -size +100M -exec ls -lh {} \;
Exercise 3: Search and Discover (10 min)#
Use grep and find to explore a real codebase.
Instructions#
Find all import statements in a Python project. Navigate to any Python project you have (or use this course directory), then run:
grep -r "import" . --include="*.py"
Count them:
grep -r "import" . --include="*.py" | wc -l
See the most common imports:
grep -r "import" . --include="*.py" | sort | head -20
Ask your AI agent:
How do I find all Python files larger than 10KB in the current directory?
Run the command it gives you. Did it work? If not, ask it to fix the command.
Bonus#
Try combining find and grep:
# Find all .py files and search for "def " in them
find . -name "*.py" -exec grep -l "def " {} \;
What does the -l flag do differently than running grep without it?
Pipes and Redirection#
The power of the command line is combining simple commands.
Pipes (|)#
Send output of one command to input of another:
# Count Python files
find . -name "*.py" | wc -l
# Find large files and sort by size
du -sh * | sort -h
# Search command history
history | grep "git"
Redirection#
# Write to file (overwrite)
echo "hello" > file.txt
# Append to file
echo "world" >> file.txt
# Redirect errors
command 2> errors.log
# Redirect both stdout and stderr
command > output.log 2>&1
AI Help for Complex Pipelines#
> I want to find all Python files, count lines in each, and show the top 10 largest
Claude:
find . -name "*.py" -exec wc -l {} \; | sort -rn | head -10
Exercise 4: Pipeline Challenge (10 min)#
Build a command pipeline from scratch.
Setup#
First, create a data file:
echo -e "banana\napple\ncherry\napple\nbanana\nbanana" > fruits.txt
Verify the contents: cat fruits.txt
Challenge#
Build a single pipeline (one line using |) that:
Counts how many times each fruit appears
Sorts by frequency (most frequent first)
Shows only the single most frequent fruit
Hints:
sortarranges lines alphabetically (needed beforeuniq)uniq -ccounts consecutive identical linessort -rnsorts numerically in reverse (largest first)head -1shows only the first line
Solution Steps#
Try building up the pipeline one step at a time:
# Step 1: Sort the fruits
cat fruits.txt | sort
# Step 2: Count duplicates
cat fruits.txt | sort | uniq -c
# Step 3: Sort by count (most frequent first)
cat fruits.txt | sort | uniq -c | sort -rn
# Step 4: Show only the top one
cat fruits.txt | sort | uniq -c | sort -rn | head -1
AI Assist#
If you got stuck, ask your AI agent: “Explain this pipeline step by step: cat fruits.txt | sort | uniq -c | sort -rn | head -1”
Clean Up#
rm fruits.txt
Environment Variables#
Viewing Variables#
# All variables
env
# Specific variable
echo $HOME
echo $PATH
echo $SHELL
Setting Variables#
# Temporary (current session)
export MY_VAR="value"
# Permanent (add to ~/.bashrc or ~/.zshrc)
echo 'export MY_VAR="value"' >> ~/.bashrc
source ~/.bashrc # Reload
The PATH Variable#
PATH tells the shell where to find executables:
# View PATH
echo $PATH
# Add to PATH
export PATH="$HOME/bin:$PATH"
Creating Shell Scripts#
Basic Script#
#!/bin/bash
# my_script.sh - A simple script
echo "Hello, $USER!"
echo "Current directory: $(pwd)"
echo "Today is: $(date)"
Making Scripts Executable#
# Add execute permission
chmod +x my_script.sh
# Run
./my_script.sh
AI-Generated Scripts#
> Create a bash script that backs up all .py files to a backup folder with today's date
Claude:
#!/bin/bash
# backup_python.sh - Backup Python files
BACKUP_DIR="backup_$(date +%Y%m%d)"
mkdir -p "$BACKUP_DIR"
find . -name "*.py" -exec cp {} "$BACKUP_DIR/" \;
echo "Backed up $(ls "$BACKUP_DIR" | wc -l) files to $BACKUP_DIR"
Shell Configuration#
Configuration Files#
File |
When Loaded |
|---|---|
|
Interactive bash shells |
|
Interactive zsh shells |
|
Login shells |
Useful Customizations#
# Aliases (shortcuts)
alias ll='ls -la'
alias gs='git status'
alias py='python3'
# Functions
mkcd() {
mkdir -p "$1" && cd "$1"
}
# Better history
export HISTSIZE=10000
export HISTFILESIZE=10000
AI Help for Configuration#
> Help me set up a nice bash prompt that shows git branch
Claude: Add to ~/.bashrc:
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "
Exercise: Build a Project Setup Script#
Use AI to create a script that:
Creates a new project directory with a given name
Sets up a Python virtual environment
Creates basic files (README.md, .gitignore)
Initializes a git repository
Prompt for AI#
Create a bash script called new_project.sh that:
- Takes a project name as argument
- Creates a directory with that name
- Creates a Python virtual environment inside it
- Creates a README.md with the project name as header
- Creates a .gitignore for Python projects
- Initializes git
- Prints success message with next steps
Review Checklist#
After AI generates the script:
Does it check if the directory already exists?
Does it handle errors?
Is the .gitignore comprehensive?
Does it work on your system?
Summary#
Concept |
Key Commands |
|---|---|
Navigation |
|
Files |
|
Search |
|
Pipes |
|
Redirection |
|
Variables |
|
Scripts |
|
AI Tip: Don’t memorize every flag and option. Learn the concepts and ask AI for specific syntax.
Tips and Tricks#
Ask AI to decode commands: Prompt “What does this command do: [paste command]” is cheaper and faster than searching documentation yourself.
Let AI write shell one-liners: Prompt “Write a bash one-liner that [task]” plays to AI’s strengths. Shell syntax is hard to memorize but easy for AI to generate.
Use
history | grepbefore asking AI: Runhistory | grep [keyword]to find commands you ran before instead of spending tokens asking the agent again.Redirect output to files for later sharing: Use
> output.txtso you can share results with the agent later without re-running expensive or slow commands.Read destructive commands before approving: When the agent suggests
rm -rf,chmod 777, or any destructive command, always read and understand it before approving.Set up shell aliases with AI help: Ask “Add aliases to my .bashrc for [common tasks]” to automate repetitive commands you type daily.
Pipe long outputs through
headortail: This saves context window space. Usehead -20ortail -20instead of dumping thousands of lines into the conversation.Memorize 5-10 core commands: Learn
cd,ls,grep,cat,mv,cp,mkdir,rm,chmod, andechoby heart. You will type these faster than asking AI.
Foundational Concepts to Commit to Memory#
Terminal vs. shell: The terminal is the window; the shell (bash, zsh) is the program interpreting your commands inside it. They are not the same thing.
Absolute vs. relative paths: Absolute paths start from root (
/home/user/file.py); relative paths start from where you are (./file.py). Confusion between the two causes most “file not found” errors.PATH is how your shell finds programs: When you type
python, the shell searches directories listed in$PATHleft to right. If your command is not found, it is a PATH problem.Pipes chain simple tools into powerful workflows:
cmd1 | cmd2sends the output of one command as input to the next. Most real-world shell work is built from small commands piped together.Redirection controls where output goes:
>writes to a file (overwriting),>>appends,2>captures errors. These are fundamental to scripting and logging.grep searches inside files; find searches for files: Know which one to reach for.
grep -r "pattern" .searches content recursively;find . -name "*.py"finds files by name.Environment variables configure your session:
$HOME,$PATH,$SHELLare set when your shell starts. You can set your own withexport. Permanent changes go in~/.bashrcor~/.zshrc.chmod +x makes scripts executable: Without execute permission, a script file is just text. The shebang line (
#!/bin/bash) tells the OS which interpreter to use.
Knowing vs. Doing: Reflection#
The terminal is one of those topics where people fall into two camps: those who spend years memorizing flags and options before they feel “ready,” and those who avoid the command line entirely because it feels arcane. Neither extreme serves you well. The right approach is to learn the core concepts – paths, pipes, redirection, environment variables – and then use AI to fill in the syntax details when you need them. You do not need to memorize find . -type f -mtime -7; you need to know that find is the tool for locating files and that you can filter by modification time. That conceptual knowledge is what lets you ask the right question.
Here is where AI agents change the game for terminal work: they are extraordinarily good at generating shell commands and scripts. You can describe what you want in plain English and get a working pipeline in seconds. That means you can accomplish things on day one that used to take months of accumulated shell knowledge. But – and this matters – if you do not understand what the command does before you run it, you are one typo away from rm -rf / or piping sensitive data somewhere it should not go. The terminal gives you real power with no safety net.
So use AI to move fast, but make it a habit to read and understand every command before you hit Enter. Ask the AI to explain commands you do not recognize. Over time, the common patterns will stick naturally because you are using them, not because you sat down to memorize them. That is the difference between learning through doing and learning through studying – and in this course, we are firmly on the doing side. There will always be more commands and more flags to discover, and that is fine. What matters is that you can get things done and that you understand what you are doing while you do it.
Additional Resources#
GNU Bash Manual – The complete reference for bash syntax, builtins, and scripting
Oh My Zsh – Framework for managing zsh configuration with themes and plugins
Zsh Official Site – Documentation and downloads for the Z Shell
Next Steps#
In the next module, we’ll learn Git version control - essential for tracking changes to your code.