Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Chapter 1: Introduction to the Command Line Interface (CLI)

ASTR 596: Modeling the Universe

San Diego State University

Before You Begin: Safety Rules

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:

The CLI Superpower

The command line gives you:

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}"; done

Understanding the Terminal (2-minute conceptual foundation)

What’s What?

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

Linux

Windows

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 Directory

Try it: Type pwd and press Enter

You should see something like:

2. What’s Here? (ls)

ls      # List files and folders

Try 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 directory

Try it:

  1. Type cd Desktop (or any folder you see from ls)

  2. Type pwd to confirm you moved

  3. Type cd .. to go back

File System Navigation

Understanding Paths

Your computer’s files are organized in a tree:

/                    # Root (top level)
├── home/           
│   └── yourname/    # Your home directory (~)
│       ├── Desktop/
│       ├── Documents/
│       └── astr596/
│           ├── project1/
│           └── project2/

Two types of paths:

~     # Your home directory
.     # Current directory  
..    # Parent directory (one up)
-     # Previous directory

Enhanced ls Commands

Now let’s add options to ls:

ls -a          # Show ALL files (including hidden)
ls -l          # Long format (details)
ls -la         # Combine: all files, detailed
ls -lh         # Human-readable sizes (KB, MB)
ls *.py        # List only Python files

Example output of ls -lh:

total 28K
-rw-r--r-- 1 user group 2.4K Nov 15 14:23 main.py
-rw-r--r-- 1 user group  15K Nov 15 14:20 nbody.py
drwxr-xr-x 2 user group 4.0K Nov 14 10:15 data/

This shows: permissions, owner, size, date, name

Pro Navigation with Tab Completion

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 once

Creating Files

touch README.md              # Create empty file
touch script.py module.py    # Create multiple files
echo "# Project 1" > README.md   # Create file with content

Copying 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)

Moving and Renaming

mv oldname.py newname.py    # Rename file
mv file.py ../              # Move up one directory
mv *.dat data/             # Move all .dat files

⚠️ Removing Files and Directories

Safe deletion examples:

rm file.py                # Remove single file
rm -i *.tmp              # Remove with confirmation
rm -r empty_folder/      # Remove empty directory

Viewing 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 search

Platform Compatibility Reference

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 mode

Managing Your Environment

conda activate astr596    # Enter course environment
conda deactivate         # Exit environment
which python            # Check which Python is active
conda list              # See installed packages

When Things Go Wrong

Next Steps

Congratulations! You now know the essential CLI commands for this course.

Your learning path:

  1. ✅ Master the first three commands (pwd, ls, cd)

  2. ✅ Practice file operations carefully

  3. ✅ Use Tab completion religiously

  4. → 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 ASTR 596.

Remember:

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!