language: en kae3g ← back to index
(unlisted) — This essay is not shown on the main index but remains accessible via direct link

kae3g 9550: The Command Line - Your Primary Interface

Phase 1: Foundations & Philosophy | Week 3 | Reading Time: 14 minutes

What You'll Learn

Prerequisites

Why Command Line?

GUIs are nice. Point, click, done. So why learn the command line?

1. Power

GUI: Can do what designer anticipated.
Command line: Can do anything the system allows.

Example:

2. Automation

GUI: Must click repeatedly (boring, error-prone).
Command line: Write script once, run forever.

#!/bin/bash
# Backup script
tar -czf backup-$(date +%Y%m%d).tar.gz ~/important-stuff/
scp backup-*.tar.gz server:/backups/

Save this script, run daily (or automate with cron). GUIs can't do this.

3. Remote Access

GUI: Requires graphical session (slow over network).
Command line: Works over SSH (text-only, fast).

# From your laptop, control a server in Virginia:
ssh user@server.example.com
# Now you're on that server's command line
# Full control, minimal bandwidth

Servers typically don't have GUIs (waste of resources). Command line is the interface.

4. Composition

GUI: Each app is isolated (can't pipe Photoshop output to Excel).
Command line: Everything composes (Unix philosophy!).

# Get top 10 most common words
cat essay.md | tr ' ' '\n' | sort | uniq -c | sort -rn | head -10

Six programs, working together. Try that in a GUI!

Essential Commands

Navigation

# Where am I?
pwd  # Print Working Directory
# => /Users/you/projects/valley

# List files
ls
ls -la  # Long format, include hidden files
ls -lh  # Human-readable sizes (5M instead of 5242880)

# Change directory
cd /path/to/directory  # Absolute path
cd relative/path       # Relative to current
cd ..                  # Parent directory
cd ~                   # Home directory
cd -                   # Previous directory (toggle!)

File Operations

# Create
touch file.txt      # Create empty file
mkdir directory     # Create directory
mkdir -p a/b/c      # Create nested directories

# Copy
cp source.txt dest.txt
cp -r dir1/ dir2/   # Recursive (for directories)

# Move/Rename
mv old.txt new.txt
mv file.txt ../    # Move to parent directory

# Delete
rm file.txt
rm -r directory/   # Recursive (careful!)
rm -i *.txt        # Interactive (confirm each)

# View
cat file.txt       # Dump entire file
less file.txt      # Page through (q to quit)
head -20 file.txt  # First 20 lines
tail -20 file.txt  # Last 20 lines

Text Processing

# Search
grep "pattern" file.txt
grep -i "pattern" file.txt  # Case-insensitive
grep -r "pattern" directory/  # Recursive

# Count
wc file.txt         # Lines, words, characters
wc -l file.txt      # Just lines

# Sort
sort file.txt
sort -n file.txt    # Numeric sort
sort -r file.txt    # Reverse

# Unique
sort file.txt | uniq       # Remove adjacent duplicates
sort file.txt | uniq -c    # Count occurrences

Paths: Absolute vs Relative

Absolute Paths

Start with / (root):

/Users/alice/documents/essay.txt
/etc/hosts
/var/log/system.log

Always works, regardless of current directory.

Use when: Scripting (scripts should work from anywhere).

Relative Paths

Relative to current directory:

# If you're in /Users/alice/
documents/essay.txt   # => /Users/alice/documents/essay.txt
../bob/file.txt       # => /Users/bob/file.txt
./script.sh           # => /Users/alice/script.sh (current dir)

Shorter, but depends on where you are.

Use when: Interactive use (less typing).

Special Paths

~        # Home directory (/Users/alice)
.        # Current directory
..       # Parent directory
-        # Previous directory (cd - to toggle)

Shell Shortcuts (Life-Changing!)

Tab Completion

cd /usr/lo[TAB]  # Completes to: cd /usr/local/

Tab completes filenames, commands, paths. Use it constantly (save 80% of typing).

History Navigation

# Up arrow: previous command
# Down arrow: next command

# Search history:
Ctrl-R  # Reverse search
# Type "grep" → shows last grep command
# Enter to run, Ctrl-R again for older matches

# View history
history
history | grep "git commit"  # Find that commit command you ran yesterday

Editing Shortcuts

Ctrl-A   # Beginning of line
Ctrl-E   # End of line
Ctrl-U   # Delete from cursor to beginning
Ctrl-K   # Delete from cursor to end
Ctrl-W   # Delete previous word
Ctrl-L   # Clear screen (like `clear` command)

Master these: Save thousands of keystrokes.

Process Control

Ctrl-C   # Kill current process (interrupt)
Ctrl-Z   # Suspend current process
fg       # Resume suspended process (foreground)
bg       # Resume in background

# Run in background from start:
long-running-command &

Pipes and Redirection

Standard Streams

Every process has three streams:

stdin  (0): Input (keyboard by default)
stdout (1): Output (terminal by default)
stderr (2): Errors (terminal by default)

Redirection

# Redirect stdout to file
ls > file-list.txt

# Append to file
echo "new line" >> log.txt

# Redirect stderr
command 2> errors.txt

# Redirect both stdout and stderr
command > output.txt 2>&1

# Redirect stdin (read from file)
sort < unsorted.txt

Pipes

# Pipe stdout of command1 to stdin of command2
command1 | command2

# Chain multiple:
cat file.txt | grep "error" | wc -l
# "How many error lines?"

Pipes are composition (Unix philosophy in action).

Practical Examples

Example 1: Find Large Files

# Find files larger than 100MB
find ~ -type f -size +100M

# Sort by size (largest first)
find ~ -type f -size +100M -exec ls -lh {} \; | sort -k5 -hr

Example 2: Monitor Disk Usage

# What's using space?
du -sh *  # Summary, human-readable, current directory

# Largest directories:
du -sh * | sort -hr | head -10

Example 3: Quick Web Server

# Python 3: serve current directory on port 8000
python3 -m http.server 8000

# Visit: http://localhost:8000
# Instant file sharing!

Example 4: Download & Extract

# Download file
curl -O https://example.com/file.tar.gz

# Extract
tar -xzf file.tar.gz

# One-liner:
curl https://example.com/file.tar.gz | tar -xz

Shell Scripting Basics

Your First Script

#!/bin/bash
# hello.sh

echo "Hello, $1!"  # $1 = first argument

Make executable:

chmod +x hello.sh
./hello.sh Alice  # => "Hello, Alice!"

Variables

name="Alice"
echo "Hello, $name"

# Command substitution
current_date=$(date +%Y-%m-%d)
echo "Today is $current_date"

Conditionals

if [ -f "file.txt" ]; then
    echo "File exists"
else
    echo "File not found"
fi

Loops

# Loop over files
for file in *.txt; do
    echo "Processing $file"
    wc -l "$file"
done

# Loop over numbers
for i in {1..10}; do
    echo "Iteration $i"
done

Customizing Your Shell

Shell Configuration

Files that run on startup:

~/.bashrc    # Bash (Linux)
~/.zshrc     # Zsh (macOS default since Catalina)
~/.profile   # Login shells

Add aliases (shortcuts):

# In ~/.zshrc or ~/.bashrc:
alias ll='ls -lah'
alias gst='git status'
alias gco='git checkout'
alias reload='source ~/.zshrc'

# Now: type `ll` instead of `ls -lah`

Prompt Customization

# Show current directory and git branch
export PS1='\w $(git branch 2>/dev/null | grep "^*" | cut -d " " -f2)\$ '

# Looks like:
# ~/projects/valley main$ 

Environment Variables

# Set PATH (where shell looks for commands)
export PATH="$HOME/bin:$PATH"

# Now commands in ~/bin/ are available

Try This

Exercise 1: Navigation Workout

Without using GUI (Finder, Explorer):

  1. Open terminal
  2. Go to home directory: cd ~
  3. List files: ls -la
  4. Create test directory: mkdir shell-practice
  5. Enter it: cd shell-practice
  6. Create files: touch file1.txt file2.txt file3.txt
  7. List them: ls
  8. Go to parent: cd ..
  9. Remove directory: rm -r shell-practice

Goal: Feel comfortable navigating via command line.

Exercise 2: Pipeline Challenge

Find the 5 largest files in your home directory:

find ~ -type f -exec ls -lh {} \; 2>/dev/null | \
    sort -k5 -hr | \
    head -5

Breakdown:

Modify: Find .txt files only, or find smallest files.

Exercise 3: Create Your Alias

Add to ~/.zshrc or ~/.bashrc:

# Your custom aliases
alias projects='cd ~/projects'
alias today='date +%Y-%m-%d'
alias count='wc -l'

# Reload:
source ~/.zshrc

Now:

Build your personal toolkit!

Advanced Patterns

1. Find and Replace Across Files

# Find all .md files containing "old text", replace with "new text"
find . -name "*.md" -exec sed -i '' 's/old text/new text/g' {} \;

# macOS: sed -i ''
# Linux: sed -i

2. Parallel Execution

# Process files in parallel (GNU parallel)
find . -name "*.jpg" | parallel convert {} {.}.png

# Or with xargs:
find . -name "*.jpg" | xargs -P 4 -I {} convert {} {}.png
# -P 4: use 4 parallel processes

3. Watch Command

# Run command every 2 seconds
watch -n 2 'df -h'

# Monitor disk space live

4. Process Substitution

# Compare output of two commands
diff <(ls dir1/) <(ls dir2/)

# <(...) creates temporary file with command output

The Shell as Programming Language

Shell (bash, zsh, fish) is a programming language:

Functions

# Define function
greet() {
    echo "Hello, $1!"
}

# Call it
greet Alice  # => "Hello, Alice!"

Error Handling

# Check if command succeeded
if git pull; then
    echo "Updated successfully"
else
    echo "Pull failed"
    exit 1
fi

# Or shorthand:
git pull && echo "Success" || echo "Failed"

Arrays

# Bash arrays
files=(file1.txt file2.txt file3.txt)

# Loop over array
for file in "${files[@]}"; do
    echo "Processing $file"
done

Going Deeper

Related Essays

External Resources

For Shell Mastery

Reflection Questions

  1. Why do developers love the command line despite its learning curve? (Power, automation, composition)
  2. Will GUIs make command lines obsolete? (Unlikely—they serve different needs)
  3. Is shell scripting "real programming"? (Yes! Bash is Turing-complete)
  4. Should non-technical users learn the command line? (Depends—but basic navigation is empowering)
  5. What's your most-used command? (Mine: git status, ls, cd)

Summary

The command line:

Essential commands:

Skills:

In the Valley:

Next: We'll explore text files—the universal data format that makes the command line so powerful. Why plain text beats binary formats for durability and interoperability!

Navigation:
← Previous: 9540 (types sets mathematical foundations) | Phase 1 Index | Next: 9560 (text files universal format)

Metadata:

Copyright © 2025 kae3g | Dual-licensed under Apache-2.0 / MIT
Competitive technology in service of clarity and beauty


← back to index