Terminal Fundamentals

Typehands-on
Duration2 hours
PrerequisitesUnit 3 — Browser DevTools Mastery

Learning Objectives

By the end of this unit, you will be able to:

  • Navigate the filesystem using command-line commands
  • Create, move, copy, and delete files and directories
  • Use essential commands for daily development tasks
  • Understand environment variables and PATH

The terminal (also called command line or shell) is where developers spend a significant amount of their time. While GUIs are intuitive, the terminal offers speed, precision, and automation that no graphical interface can match. Mastering basic terminal commands will make you a more efficient developer.

Terminal Basics

A shell is the program that processes your commands. The most common shells are bash (default on Linux/older macOS) and zsh (default on newer macOS). Windows uses PowerShell or cmd, but many developers install WSL (Windows Subsystem for Linux) for a Unix-like experience.

TermDefinition
ShellThe command interpreter (bash, zsh, fish)
TerminalThe application window running the shell
PromptThe text showing you can type (usually $ or %)
CommandAn instruction you type and execute
ArgumentsAdditional info passed to a command
Flags/OptionsModifiers like -r or --verbose

Think of the shell as a translator between you and the operating system. You type human-readable commands; it converts them into actions the OS understands.

Navigation Commands

The filesystem is a tree structure with directories (folders) containing files and subdirectories. Navigation commands let you move around this tree.

CommandDescriptionExample
pwdPrint working directory (where you are)pwd/Users/alice/projects
lsList directory contentsls -la (detailed with hidden files)
cdChange directorycd projects or cd .. (parent)
cd ~Go to home directorycd ~/Users/alice
cd -Go to previous directoryToggle between two directories

Path Types

TypeStarts WithExample
Absolute Path/ (root)/Users/alice/projects/app
Relative PathCurrent directory./src/index.js or ../config
Home Shortcut~~/Documents
# Navigation examples
pwd # Where am I?
ls # What's here?
ls -la # Detailed list including hidden files
cd ~/projects # Go to projects folder
cd .. # Go up one level
cd - # Go back to previous directory
Filesystem tree diagram showing directory structure
Figure 1: Unix Filesystem Hierarchy — Navigating the Directory Tree

File Operations

CommandDescriptionExample
touchCreate empty filetouch index.html
mkdirCreate directorymkdir -p src/components
cpCopy files/directoriescp file.txt backup.txt
mvMove or renamemv old.js new.js
rmRemove filesrm file.txt
rm -rfRemove directory recursivelyrm -rf node_modules

rm -rf is dangerous! There's no trash can — files are permanently deleted. Always double-check the path before running this command.

# File operations
touch README.md # Create empty file
mkdir -p src/components # Create nested directories
cp config.js config.backup.js # Copy file
mv old-name.js new-name.js # Rename file
mv file.js src/ # Move file to directory
rm -rf node_modules # Delete directory (careful!)

Viewing Files

CommandDescriptionBest For
catOutput entire fileSmall files
lessScrollable viewerLarge files (q to quit)
headFirst N lineshead -20 file.txt
tailLast N linestail -f server.log (live follow)
wcCount lines/words/charswc -l file.txt (line count)
# Viewing files
cat package.json # Print entire file
less very-long-file.txt # Scrollable view (q to quit)
head -20 log.txt # First 20 lines
tail -f server.log # Follow new lines in real-time
wc -l src/*.js # Count lines in all JS files

Searching and Filtering

CommandDescriptionExample
findFind files by name/typefind . -name "*.js"
grepSearch inside filesgrep "TODO" src/*.js
grep -rRecursive searchgrep -r "console.log" .
| (pipe)Chain commandscat log.txt | grep ERROR
# Searching
find . -name "*.tsx" # Find all TSX files
find . -type d -name "node_modules" # Find directories named node_modules
grep "import" src/index.js # Search for "import" in file
grep -r "TODO" --include="*.js" . # Recursive search in JS files
history | grep "npm" # Search command history

Environment Variables

Environment variables store configuration that programs can read. The PATH variable is especially important — it tells the shell where to find executable programs.

CommandDescription
echo $PATHPrint PATH variable
echo $HOMEPrint home directory
export VAR=valueSet environment variable (current session)
envList all environment variables
which nodeShow path to an executable
# Environment variables
echo $PATH # Show executable search paths
echo $HOME # Home directory
export NODE_ENV=development # Set variable for session
which node # Where is node installed?
which npm # Where is npm installed?

Add permanent environment variables to your shell config file: ~/.zshrc (zsh) or ~/.bashrc (bash). Run source ~/.zshrc to reload after editing.

Useful Shortcuts

ShortcutAction
TabAuto-complete file/command names
Ctrl+CCancel current command
Ctrl+LClear screen (same as clear)
Ctrl+RSearch command history
Ctrl+AMove cursor to start of line
Ctrl+EMove cursor to end of line
!!Repeat last command
!$Last argument of previous command
Terminal keyboard shortcuts cheat sheet
Figure 2: Essential Terminal Keyboard Shortcuts

Hands-On: Terminal Practice

Exercise: Navigate the Filesystem

  1. Open your terminal
  2. Run pwd to see your current location
  3. Run ls -la to see all files including hidden ones
  4. Navigate to your home directory with cd ~
  5. Go to your Documents folder with cd Documents
  6. Return to your previous directory with cd -

Exercise: Create a Project Structure

  1. Create a new directory: mkdir practice-project
  2. Navigate into it: cd practice-project
  3. Create nested folders: mkdir -p src/components src/utils
  4. Create files: touch src/index.js src/components/App.js README.md
  5. Verify structure: find . -type f
  6. View a file: cat README.md

Exercise: Search and Filter

  1. Navigate to any project with JavaScript files
  2. Find all .js files: find . -name "*.js" | head -10
  3. Search for imports: grep -r "import" --include="*.js" . | head -10
  4. Check your command history: history | tail -20
  5. Search history for a command: history | grep "cd"

Key Takeaways

  1. Use `pwd`, `ls`, and `cd` to navigate the filesystem
  2. Create files with `touch`, directories with `mkdir -p`
  3. Move/rename with `mv`, copy with `cp`, delete with `rm`
  4. View files with `cat`, `less`, `head`, `tail`
  5. Search files with `find`, search content with `grep`
  6. Environment variables like `PATH` control where the shell finds programs
  7. Use `Tab` for autocomplete and `Ctrl+R` to search command history