Chapter 1: Introduction to the Command Line Interface (CLI)
COMP 536: Computational Modeling for Scientists
Before You Begin: Safety Rules
The terminal is powerful but unforgiving. Follow these rules ALWAYS:
- pwd before rm - Always know where you are before deleting
- ls before rm - Always see what you’ll delete
- Use Tab completion - Reduces dangerous typos
- Keep backups - The terminal has NO undo button
- Think twice, type once - Especially with delete commands
Remember: One wrong character can delete everything. There is no recycle bin.
Why Use the Terminal?
The GUI Limitation
When you use Finder (macOS) or File Explorer (Windows), you’re limited to what the designers decided to show you. Want to:
- Rename 1000 files at once? Good luck clicking each one.
- Find all Python files modified in the last week? No easy way.
- Run your code on a supercomputer? There’s no GUI there.
- Process data on a remote server? You need the terminal.
The CLI Superpower
The command line gives you:
- Automation: Do repetitive tasks in seconds, not hours
- Remote access: Control computers anywhere in the world
- Power: Access to thousands of tools not available in GUIs
- Speed: Keyboard is faster than mouse for many tasks
- Reproducibility: Save and share exact commands you ran
- Professional necessity: Every computational scientist uses it
Real example: Renaming simulation outputs
# GUI way: Click each file, rename manually (30 minutes for 100 files)
# CLI way: One command, 2 seconds (we'll learn how later)
for i in *.dat; do mv "$i" "simulation_${i}"; doneUnderstanding the Terminal (2-minute conceptual foundation)
What’s What?
- Terminal: The application you open (like Terminal.app on Mac)
- Shell: The program that interprets your commands (bash, zsh, etc.)
- Command Line: Where you type commands
- Prompt: Shows you’re ready for input (usually ends with $ or >)
How Commands Work
command -options arguments
^ ^ ^
what how to what to
to do do it do it on
Example: ls -la /home means “list (ls) with long format and all files (-la) in the /home directory”
Getting Started: Opening the Terminal
macOS
- Press
Cmd + Space, type “Terminal”, press Enter - Or: Applications \(\to\) Utilities \(\to\) Terminal
Linux
- Press
Ctrl + Alt + T - Or: Look for “Terminal” in applications
Windows
- Best option: Use “Git Bash” (installed with Git)
- Alternative: Windows Terminal or PowerShell
- Avoid: Command Prompt (cmd.exe) - uses different commands
If using Git Bash on Windows:
- Some commands work differently (we’ll note these)
- No
mancommand (use--helpinstead) - Path separators: Use
/not\ - Your home is
/c/Users/YourNamenotC:\Users\YourName
Your First Three Commands
Master these three before anything else. Seriously, practice just these for 10 minutes.
1. Where Am I? (pwd)
pwd # Print Working DirectoryTry it: Type pwd and press Enter
You should see something like:
- macOS/Linux:
/Users/yournameor/home/yourname - Windows Git Bash:
/c/Users/yourname
2. What’s Here? (ls)
ls # List files and foldersTry it: Type ls and press Enter
You’ll see files and folders in your current location.
3. Move Around (cd)
cd Desktop # Change Directory to Desktop
cd .. # Go up one level
cd ~ # Go to your home directoryTry it:
- Type
cd Desktop(or any folder you see fromls) - Type
pwdto confirm you moved - Type
cd ..to go back
Can you:
If yes, you’ve mastered the basics! If no, practice for 5 more minutes.
✅ Mastery Checklist (you’re “done” when…)
Before moving on, you should be able to do each of these without guessing:
If any box feels shaky, that’s normal — spend 5 extra minutes practicing just that one skill. Fluency comes from repetition, not reading.
File and Directory Operations
Creating Directories
mkdir project3 # Make one directory
mkdir -p data/raw/2024 # Make nested directories
mkdir results plots analysis # Make multiple at onceCreating Files
touch README.md # Create empty file
touch script.py module.py # Create multiple files
echo "# Project 1" > README.md # Create file with contentCopying Files
cp file1.py file2.py # Copy file
cp file1.py backup/ # Copy to directory
cp -r project1/ project1_backup/ # Copy entire directory (-r = recursive)Before modifying important files:
cp important.py important.py.backupNow you can recover if something goes wrong!
Moving and Renaming
mv oldname.py newname.py # Rename file
mv file.py ../ # Move up one directory
mv *.dat data/ # Move all .dat filesRemoving Files and Directories
THE rm COMMAND IS PERMANENT. NO UNDO. NO RECYCLE BIN.
NEVER EVER use these:
rm -rf /= DELETE ENTIRE COMPUTERrm -rf ~= DELETE ALL YOUR FILESrm -rf *= DELETE EVERYTHING IN CURRENT FOLDER
What -rf means:
-r= recursive (delete folders and everything inside)-f= force (no confirmation, even for important files)
Safe practices:
- Use
rm -ifor interactive mode (asks confirmation) - Use
lsfirst to see what you’ll delete - Use
pwdto confirm you’re in the right place - Start with
rm(no flags) for single files
Safe deletion examples:
rm file.py # Remove single file
rm -i *.tmp # Remove with confirmation
rm -r empty_folder/ # Remove empty directoryViewing Files
Quick Views
cat file.py # Show entire file
head file.py # Show first 10 lines
head -n 20 file.py # Show first 20 lines
tail file.py # Show last 10 lines
tail -f output.log # Follow file updates (great for logs)
less bigfile.txt # Page through file (q to quit)Searching in Files
grep "import" *.py # Find "import" in Python files
grep -n "error" log.txt # Show with line numbers
grep -r "TODO" . # Search all files recursively
grep -i "warning" log # Case-insensitive searchPlatform Compatibility Reference
| Command | macOS/Linux | Git Bash | Windows CMD | Alternative |
|---|---|---|---|---|
ls |
Works | Works | No | Use dir |
pwd |
Works | Works | No | Use cd (no args) |
cat |
Works | Works | No | Use type |
rm |
Works | Works | No | Use del |
man |
Works | No | No | Use --help |
grep |
Works | Works | No | Use findstr |
ps aux |
Works | Limited | No | Use tasklist |
Tip: When in doubt, use command --help to see options
Working with Python
Running Python Scripts
python script.py # Run script
python -m module # Run module
python -c "print('hello')" # Run one line
python # Interactive mode (exit() to quit)
ipython # Better interactive modeManaging Your Environment
conda activate comp536 # Enter course environment
conda deactivate # Exit environment
which python # Check which Python is active
conda list # See installed packagesWhen Things Go Wrong
“command not found”
- Typo in command name
- Command not installed
- Not in PATH
- Fix: Check spelling, install missing tool
“permission denied”
- Need admin/sudo rights
- File is protected
- Wrong ownership
- Fix: Use sudo (carefully!) or check file permissions
“no such file or directory”
- Wrong path or filename
- In wrong directory
- File doesn’t exist
- Fix: Check with
pwdandls, verify path
“syntax error near unexpected token”
- Special character not escaped
- Quote mismatch
- Wrong shell syntax
- Fix: Check quotes and special characters
Process running forever
- Press
Ctrl+Cto stop - If frozen, try
Ctrl+Zthenkill %1
Input/Output Redirection
Saving Output
python script.py > output.txt # Save output to file
python script.py >> output.txt # Append to file
python script.py 2> errors.txt # Save errors only
python script.py &> all.txt # Save everythingPipes (Combining Commands)
ls -la | grep ".py" # List files, filter Python
cat data.txt | sort | uniq # Sort and remove duplicates
history | grep "git" # Find git commandsUseful Shortcuts and Tips
Keyboard Shortcuts
Tab # AUTOCOMPLETE (use constantly!)
^/v # Previous/next command
Ctrl+C # Stop current command
Ctrl+L # Clear screen
Ctrl+A # Go to line beginning
Ctrl+E # Go to line end
Ctrl+R # Search command history
Ctrl+D # Exit/logout
Wildcards (Pattern Matching)
* # Any characters
? # Single character
[abc] # Any of a, b, c
[0-9] # Any digit
# Examples:
ls *.py # All Python files
ls data_?.txt # data_1.txt, data_2.txt, etc.
ls img_[0-9][0-9].png # img_00.png through img_99.pngQuick Reference Card
Essential Daily Commands
# Navigation
pwd # Where am I?
ls -la # What's here? (all files, detailed)
cd folder/ # Enter folder
cd .. # Go up one level
cd ~ # Go home
# Files & Directories
mkdir project # Create directory
touch file.py # Create empty file
cp source dest # Copy
mv old new # Move/rename
rm file # Delete (CAREFUL!)
# Viewing Files
cat file # Show entire file
head -20 file # Show first 20 lines
tail -f log # Follow log file
grep "text" file # Search in file
# Python & Course
python script.py # Run Python
conda activate comp536 # Enter environment
git status # Check git
git add . && git commit -m "msg" && git push # Submit work
# Getting Help
command --help # See command options (not 'man' on Windows)🧭 Mini-Quest: The 5-Minute CLI Scavenger Hunt
This is a quick “proof to yourself” that you can navigate and manipulate files safely.
Goal: create a tiny project folder, generate a few files, organize them, and produce a screenshot-worthy final ls -la.
- Make a sandbox and enter it
cd ~
mkdir -p comp536_cli_quest/{data,code,notes}
cd comp536_cli_quest- Create a few files
touch notes/todo.txt
touch code/main.py code/utils.py
touch data/raw1.txt data/raw2.txt- Write one line into your todo file
echo "Quest complete: I can navigate and organize files safely." > notes/todo.txt- Verify what you built (the “evidence”)
pwd
ls -la
ls -la code data notes- Bonus (optional): find all Python files
ls code/*.py✅ You’re done when: you can explain what each command did and your folder structure matches the three subfolders (code/, data/, notes/).
Practice Exercises
Exercise 2: File Management (15 min)
- Create a project structure:
mkdir -p project/{src,data,docs}- Create files in each folder
- Copy a file between folders
- Rename a file
- Safely delete a test file
Exercise 3: Real Task - Organize Files (20 min)
# Create messy folder
mkdir messy && cd messy
touch file1.py file2.py data1.txt data2.txt image1.png image2.png
# Now organize them
mkdir {code,data,images}
mv *.py code/
mv *.txt data/
mv *.png images/
# Verify organization
ls -la */Practical Examples for COMP 536
Setting Up a New Project
# Create full project structure
mkdir -p project2/{src,data,outputs,docs}
cd project2
touch README.md requirements.txt
touch src/{main.py,stellar.py,utils.py}
echo "# Project 2: N-Body Simulation" > README.mdRunning and Logging Simulations
# Run with timing
time python nbody_sim.py
# Run with output capture
python nbody_sim.py > output.log 2>&1
# Run multiple parameters
for n in 100 500 1000; do
python nbody.py --particles=$n > results_n$n.txt
doneData Processing Pipeline
# Process all data files
for file in data/*.txt; do
python process.py "$file" > "processed/$(basename $file)"
done
# Check results
grep "converged" processed/*.txt | wc -lEmergency Recovery
If you’re confused or lost:
- Where am I? \(\to\)
pwd - Go home \(\to\)
cd ~ - See what’s here \(\to\)
ls -la - Stop running process \(\to\)
Ctrl+C - Clear messy screen \(\to\)
Ctrl+Lorclear - Exit and start over \(\to\)
exitand reopen terminal
Remember: Closing and reopening terminal resets everything!
Next Steps
Congratulations! You now know the essential CLI commands for this course.
Your learning path:
- Master the first three commands (pwd, ls, cd)
- Practice file operations carefully
- Use Tab completion religiously
- \(\to\) Continue to course projects
Optional Advanced Topics: Curious about remote computing (SSH), long-running jobs (screen/tmux), or shell scripting? Those are useful for research but not needed for COMP 536.
Remember:
- Tab is your friend: Saves typing and prevents errors
- pwd before rm: Always know where you are
- Be paranoid with rm: No undo in terminal!
- Practice daily: 10 minutes/day for two weeks = mastery
The command line may seem “old school” and scary at first, but it’s just typing commands instead of clicking. Within two weeks of daily use, it’ll feel natural. You’ve got this!