Chapter 1: Introduction to the Command Line Interface (CLI)
ASTR 596: Modeling the Universe
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:
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 onExample: 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 EnterOr: Applications → Utilities → Terminal
Linux¶
Press
Ctrl + Alt + TOr: 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
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/yournameWindows 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 movedType
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:
Absolute: Full path from root
/home/yourname/DesktopRelative: Path from where you are
Desktopor../Documents
Navigation Shortcuts¶
~ # Your home directory
. # Current directory
.. # Parent directory (one up)
- # Previous directoryEnhanced 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 filesExample 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 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)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 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¶
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 astr596 # Enter course environment
conda deactivate # Exit environment
which python # Check which Python is active
conda list # See installed packagesWhen Things Go Wrong¶
🔧 Understanding Error Messages
“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 stopIf frozen, try
Ctrl+Zthenkill %1
## Input/Output Redirection
### Saving Output
```bash
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!)
↑/↓ # 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/logoutWildcards (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 astr596 # 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)Practice Exercises¶
Exercise 1: Navigation Basics (10 min)¶
# 1. Find where you are
pwd
# 2. Go to your home directory
cd ~
# 3. Create a practice folder
mkdir cli_practice
cd cli_practice
# 4. Create some files
touch data1.txt data2.txt script.py
# 5. List what you created
ls -la
# 6. Go back home
cd ..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 ASTR 596¶
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¶
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
→ 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:
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!