Command Line Basics
Table of Contents
The command line is where developers get things done efficiently. While GUIs are great for discovery, the terminal is faster for repetitive tasks, essential for server management, and required for most development tools (Git, npm, Docker, deployment scripts).
This tutorial covers the commands you’ll use daily, regardless of whether you’re on macOS, Linux, or Windows (WSL).
Getting Started
Open your terminal:
- macOS: Terminal.app or iTerm2
- Linux: Your distribution’s terminal emulator
- Windows: Windows Terminal with WSL, or Git Bash
You’ll see a prompt like user@hostname:~$. The ~ means you’re in your home directory.
Navigating the File System
# Where am I?
pwd
# /home/alice
# What's in this directory?
ls
ls -la # detailed list including hidden files
ls -lh # human-readable file sizes
# Change directory
cd Documents # go into Documents
cd .. # go up one level
cd ~ # go to home directory
cd / # go to root
cd - # go to previous directory
Path types
- Absolute: starts with
/—/home/alice/projects/app - Relative: starts from current directory —
projects/appor../other-folder
Working with Files and Directories
# Create directories
mkdir projects
mkdir -p projects/my-app/src # create nested directories
# Create files
touch index.html # create empty file
echo "hello" > file.txt # create file with content
# Copy
cp file.txt backup.txt # copy file
cp -r folder/ folder-backup/ # copy directory recursively
# Move / rename
mv old-name.txt new-name.txt # rename
mv file.txt Documents/ # move to another directory
# Delete
rm file.txt # delete file
rm -r folder/ # delete directory and contents
rm -i file.txt # ask for confirmation
rm is permanent — there’s no trash can. Double-check before running rm -r on anything important. Consider using rm -i (interactive) for safety.
Reading Files
# Display entire file
cat file.txt
# Display with line numbers
cat -n file.txt
# First/last lines
head -20 file.txt # first 20 lines
tail -20 file.txt # last 20 lines
tail -f logfile.log # follow (watch for new lines in real-time)
# Page through a long file
less file.txt # scroll with arrows, q to quit
# Word/line/character count
wc file.txt # lines, words, characters
wc -l file.txt # just line count
Searching
find — locate files
# Find by name
find . -name "*.js" # all .js files in current directory tree
find /home -name "config.yml" # search from /home
# Find by type
find . -type d -name "node_modules" # directories only
find . -type f -name "*.log" # files only
# Find by modification time
find . -mtime -7 -name "*.md" # modified in last 7 days
grep — search file contents
# Search for text in files
grep "TODO" *.js # search in all .js files
grep -r "import" src/ # recursive search in directory
grep -rn "error" logs/ # show line numbers
grep -ri "password" . # case-insensitive
# Invert match (lines that DON'T contain pattern)
grep -v "debug" app.log
# Count matches
grep -c "error" app.log
Pipes and Redirection
The real power of the command line: combining simple commands into powerful pipelines.
Pipes (|) — send output of one command to another
# Find the 10 largest files
ls -lS | head -10
# Count how many .js files exist
find . -name "*.js" | wc -l
# Search command history
history | grep "docker"
# Sort and deduplicate
cat names.txt | sort | uniq
# Find processes using a port
lsof -i :3000 | grep LISTEN
Redirection
# Write output to file (overwrite)
echo "hello" > file.txt
ls -la > directory-listing.txt
# Append to file
echo "another line" >> file.txt
# Redirect errors
command 2> errors.log
# Redirect both stdout and stderr
command > output.log 2>&1
Process Management
# See running processes
ps aux # all processes
ps aux | grep node # filter for node processes
# Real-time process monitor
top # basic
htop # better (install with apt/brew)
# Kill a process
kill 12345 # graceful shutdown (SIGTERM)
kill -9 12345 # force kill (SIGKILL)
killall node # kill all processes named "node"
# Run in background
long-command & # run in background
jobs # list background jobs
fg %1 # bring job 1 to foreground
Permissions
# View permissions
ls -la
# -rw-r--r-- 1 alice staff 1024 May 18 14:00 file.txt
# drwxr-xr-x 3 alice staff 96 May 18 14:00 folder/
# Change permissions
chmod 755 script.sh # rwxr-xr-x (owner: all, others: read+execute)
chmod +x script.sh # add execute permission
chmod 644 file.txt # rw-r--r-- (owner: read+write, others: read)
# Change ownership
chown alice:staff file.txt
Permission digits: read=4, write=2, execute=1. Add them up: 7=rwx, 6=rw-, 5=r-x, 4=r–.
Environment Variables
# View all
env
printenv
# View one
echo $HOME
echo $PATH
# Set temporarily (current session only)
export API_KEY="abc123"
# Set permanently (add to ~/.bashrc or ~/.zshrc)
echo 'export API_KEY="abc123"' >> ~/.zshrc
source ~/.zshrc # reload without restarting terminal
Useful Everyday Commands
# Disk usage
df -h # filesystem usage
du -sh * # size of each item in current directory
du -sh node_modules/ # size of a specific directory
# Network
curl https://api.example.com # make HTTP requests
curl -s https://api.example.com | jq . # pretty-print JSON
ping google.com # test connectivity
wget https://example.com/file # download a file
# Archives
tar -czf archive.tar.gz folder/ # create compressed archive
tar -xzf archive.tar.gz # extract archive
zip -r archive.zip folder/ # create zip
unzip archive.zip # extract zip
# System info
uname -a # OS info
whoami # current user
date # current date/time
uptime # how long system has been running
Customizing Your Shell
Add aliases to ~/.bashrc or ~/.zshrc for commands you use often:
# Shortcuts
alias ll="ls -la"
alias gs="git status"
alias dc="docker compose"
alias ..="cd .."
alias ...="cd ../.."
# Safety nets
alias rm="rm -i"
alias mv="mv -i"
Command Cheat Sheet
| Task | Command |
|---|---|
| Where am I? | pwd |
| List files | ls -la |
| Change directory | cd path |
| Create directory | mkdir -p path |
| Create file | touch file |
| Read file | cat file or less file |
| Search in files | grep -r "text" . |
| Find files | find . -name "*.ext" |
| Move/rename | mv source dest |
| Copy | cp source dest |
| Delete | rm file or rm -r dir |
| Running processes | ps aux |
| Kill process | kill PID |
| Disk space | df -h / du -sh * |
| Download | curl URL or wget URL |
What’s Next
With command-line basics down, you’re ready for Git (version control from the terminal), Docker (containerization), or bash scripting to automate repetitive tasks.