Intermediate 45 min Mac & PC

Command Line Basics

Get comfortable with the terminal for Python development, package installation, and Git version control.

What You'll Learn:

  • Navigate file systems efficiently using command line
  • Install and manage Python packages with pip
  • Run Python scripts and troubleshoot common issues
  • Use basic Git commands for version control

The command line might seem intimidating, but it’s an essential tool for digital humanities work. When you start using Python for text analysis, data visualization, and mapping, you’ll find that many tasks are easier and faster with command line skills.

This guide will take you from command line beginner to confident user, with a focus on the tasks that matter most for DH projects. By the end, you’ll be comfortable installing Python packages, running scripts, managing files, and using basic Git commands for version control.

What You’ll Accomplish

🎯 By the end of this guide, you'll be able to:

  • Navigate efficiently through your file system using commands
  • Install Python libraries quickly with pip
  • Run Python scripts and handle common errors
  • Use Git for version control in your projects
  • Troubleshoot issues that commonly arise in DH workflows
  • Work confidently with both Mac and PC command line environments

📋 What You Need

  • A computer (Mac or PC) - we'll cover both platforms
  • No prior command line experience required
  • Willingness to type commands and experiment safely

Why Learn the Command Line?

Reality Check: Command Line Benefits

You need to install 5 Python libraries for a text analysis project. What's the fastest way?

Real DH Use Cases

  • Installing Python libraries: pip install pandas matplotlib
  • Running analysis scripts: python analyze_corpus.py
  • Converting file formats: pandoc input.md -o output.pdf
  • Downloading datasets: curl -O https://example.com/data.csv
  • Version control: git add . and git commit -m "Updated analysis"

Getting Started by Platform

🍎 Mac: Terminal

Opening Terminal:

  • Spotlight Search: ⌘+Space, type "Terminal"
  • Applications: Applications → Utilities → Terminal
  • Launchpad: Look for Terminal icon

What you'll see:

YourName@YourMac ~ %

This is your "prompt" - it's waiting for commands.

🪟 PC: PowerShell (Recommended)

Opening PowerShell:

  • Start Menu: Type "PowerShell"
  • Windows Key + X: Select "Windows PowerShell"
  • File Explorer: Shift+Right-click in any folder → "Open PowerShell window here"

What you'll see:

PS C:\Users\YourName>

This is your PowerShell prompt.

💡 Why PowerShell over Command Prompt?

PowerShell is more powerful and uses some Unix-like commands that work on Mac/Linux too.

Essential Navigation Commands

🧭 Interactive Navigation Practice

Let's practice the core navigation commands. Try these in the terminal simulator below:

1. Check Current Location
Mac/Linux: pwd
PC: Get-Location or pwd

Shows your current directory (where you are in the file system)

2. List Contents
Mac/Linux: ls or ls -la
PC: dir or Get-ChildItem

Shows files and folders in current directory

3. Change Directory
Both: cd Documents
Go up: cd ..
Go home: cd ~ (Mac) or cd $env:USERPROFILE (PC)

Navigate between folders

🖥️ Practice Terminal

Try the navigation commands above. Type help for available commands.

Terminal
Welcome to the practice terminal!
Try: pwd, ls, cd Documents, cd ..
user@computer:~$

File Operations

Creating and Managing Files

📁 File & Folder Operations

Create File:
Mac/Linux: touch filename.txt
PC: New-Item filename.txt
Creates an empty file
Create Folder:
Both: mkdir foldername
Creates a new directory
Copy Files:
Mac/Linux: cp file1.txt file2.txt
PC: Copy-Item file1.txt file2.txt
Duplicates a file
Move/Rename:
Mac/Linux: mv oldname.txt newname.txt
PC: Move-Item oldname.txt newname.txt
Moves or renames files

Python from the Command Line

Installing Python Packages

🐍 Package Installation Practice

Let's practice installing Python packages for DH work:

Scenario 1: Text Analysis Setup

You need pandas for data analysis and nltk for natural language processing.

pip install pandas nltk
Scenario 2: Data Visualization

You want to create charts and plots for your research.

pip install matplotlib seaborn plotly
Scenario 3: Web Scraping

You need to collect data from historical newspaper websites.

pip install requests beautifulsoup4 selenium

Running Python Scripts

📜 Script Execution Workflow

1
Navigate to Your Script
# Navigate to your project folder
cd ~/Documents/DH-Projects/text-analysis
      
# Verify your script is there
ls *.py
2
Run the Script
# Basic execution
python analyze_text.py

# With arguments
python analyze_text.py --input data.csv --output results.json

# Show output and save to file
python analyze_text.py | tee analysis_log.txt
3
Handle Common Issues
# If "python" command not found
python3 analyze_text.py

# If wrong Python version
which python
python --version

# If module not found
pip list | grep pandas
pip install pandas

Git Basics for DH Projects

Why Git Matters in DH

Version Control Reality Check

You've been working on a Python script for 2 weeks. Today you made changes that broke everything, and you can't remember what you changed. What would Git help you do?

Essential Git Commands

📚 Basic Git Workflow for DH Projects

1. Initialize Repository
# In your project folder
git init
git config user.name "Your Name"
git config user.email "your.email@university.edu"
2. Track Changes
# See what's changed
git status

# Add files to track
git add analyze_script.py
git add data/cleaned_texts.csv

# Add everything
git add .
3. Save Snapshots
# Commit with descriptive message
git commit -m "Add text cleaning script"
git commit -m "Fix encoding issue in data processing"
git commit -m "Complete sentiment analysis implementation"
4. View History
# See commit history
git log --oneline

# See what changed in last commit
git show

# Compare current version to last commit
git diff HEAD~1

Troubleshooting Common Issues

Problem 1: “Command not found”

🚨 Error: 'python' is not recognized

Solutions to try:
  1. Try python3: python3 --version
  2. Check installation: which python (Mac) or where python (PC)
  3. Add to PATH: Your Python installation might not be in your system PATH
  4. Reinstall: Download from python.org and check "Add to PATH"

Problem 2: “Permission denied”

🚨 Error: Permission denied

Solutions:
  • Mac/Linux: Use sudo carefully: sudo pip install package
  • Better approach: Use virtual environments or user installs: pip install --user package
  • PC: Run PowerShell as Administrator (right-click → "Run as administrator")

Problem 3: “Module not found”

🚨 Error: ModuleNotFoundError: No module named 'pandas'

Debug process:
# Check what's installed
pip list

# Check Python version
python --version

# Install missing module
pip install pandas

# If using multiple Python versions
python -m pip install pandas

Hands-On Exercise: Complete DH Workflow

🎯 Exercise: Set Up a Text Analysis Project

Let's walk through a complete workflow using command line skills:

Validation Commands:
# Check your project structure
ls -la

# Verify Git is tracking
git status

# Check installed packages
pip list | grep -E "(pandas|nltk|matplotlib)"

Advanced Tips for Efficiency

Command History and Shortcuts

⚡ Productivity Boosters

History Navigation
  • Previous command
  • Next command
  • history See all recent commands
  • Ctrl+R Search command history
Tab Completion
  • Tab Complete file/folder names
  • TabTab Show all options
  • Works with commands too!
Useful Shortcuts
  • Ctrl+C Stop running command
  • Ctrl+L Clear screen
  • Ctrl+A Go to line start
  • Ctrl+E Go to line end

Combining Commands

🔗 Command Chaining for DH Tasks

```bash # Download and immediately extract data curl -O https://example.com/corpus.zip && unzip corpus.zip # Run analysis and save output python analyze.py > results.txt 2>&1 # Process multiple files for file in *.txt; do python clean_text.py "$file"; done # Chain with conditional execution python preprocess.py && python analyze.py && python visualize.py ```

Knowledge Check

Proficiency Check: Command Line Skills

You want to run a Python script called sentiment_analysis.py that's in your scripts folder, but you're currently in your project root directory. What's the most efficient command?

Next Steps

With command line skills mastered, you’re ready to tackle text encoding and character sets, where you’ll learn to handle multilingual and historical texts without garbled characters.

Quick Reference Card

📋 Command Line Cheat Sheet

Navigation
  • pwd - Where am I?
  • ls / dir - What's here?
  • cd folder - Go somewhere
  • cd .. - Go up one level
Python
  • python script.py - Run script
  • pip install package - Install library
  • pip list - Show installed
  • python --version - Check version
Git Basics
  • git status - What changed?
  • git add . - Stage all changes
  • git commit -m "msg" - Save snapshot
  • git log --oneline - See history
Help & Shortcuts
  • Tab - Auto-complete
  • - Previous command
  • Ctrl+C - Stop command
  • command --help - Get help

Remember: The command line becomes second nature with practice. Start with these basics and gradually add more advanced techniques as you become comfortable.