📟 CLI Command Reference

Terminal commands study guide — Joaquin Jimenez

⚡ Essential Shortcuts

Ctrl+C — Cancel
Ctrl+L — Clear screen
Ctrl+A — Start of line
Ctrl+E — End of line
Tab — Autocomplete
↑ / ↓ — History
Ctrl+R — Search history
Ctrl+D — Exit/EOF
📂

Navigation

pwd Print working directory
Essential

Shows the full path of your current directory. Think of it as "Where am I right now?"

pwd
Example:
$ pwd
/home/joaquin/projects
cd Change directory
Essential

Navigate between directories. The most fundamental navigation command.

cd [directory]
Examples:
cd /home/joaquin # Go to absolute path
cd projects # Go to subdirectory
cd .. # Go up one level
cd ~ # Go to home directory
cd - # Go to previous directory
cd # Also goes to home
ls List directory contents
Essential

Lists files and directories. One of the most frequently used commands.

ls [options] [path]
Examples:
ls # List current directory
ls -l # Long format with details
ls -la # Include hidden files
ls -lh # Human-readable sizes
ls *.js # List only .js files

Common Flags

-l Long format (permissions, owner, size, date)
-a Show hidden files (starting with .)
-h Human-readable file sizes (KB, MB, GB)
-R Recursive (list subdirectories too)
-t Sort by modification time
📄

File Operations

touch Create empty file or update timestamp
Essential

Creates a new empty file, or updates the timestamp of an existing file.

touch [filename]
Examples:
touch newfile.txt # Create new file
touch file1.js file2.js # Create multiple
touch existing.txt # Update timestamp
mkdir Make directory
Essential

Creates new directories (folders).

mkdir [options] [directory]
Examples:
mkdir projects # Create directory
mkdir -p parent/child/grandchild # Create nested dirs
mkdir dir1 dir2 dir3 # Create multiple

Common Flags

-p Create parent directories as needed
-v Verbose output
cp Copy files and directories
Essential

Copies files or directories from one location to another.

cp [options] source destination
Examples:
cp file.txt backup.txt # Copy file
cp file.txt /path/to/dir/ # Copy to directory
cp -r folder/ newfolder/ # Copy directory recursively
cp *.js backup/ # Copy all .js files

Common Flags

-r Recursive (copy directories)
-i Interactive (ask before overwrite)
-v Verbose output
mv Move or rename files
Essential

Moves files to a different location OR renames them. Same command for both!

mv [options] source destination
Examples:
mv old.txt new.txt # Rename file
mv file.txt /path/to/dir/ # Move to directory
mv folder/ /new/location/ # Move directory
mv *.js src/ # Move multiple files

Common Flags

-i Interactive (ask before overwrite)
-n No overwrite (never overwrite existing)
rm Remove files and directories
Essential

⚠️ Permanently deletes files and directories. No trash can! Be careful.

rm [options] file(s)
Examples:
rm file.txt # Delete file
rm -r folder/ # Delete directory and contents
rm -i *.txt # Interactive delete
rm -rf node_modules/ # Force delete (careful!)

Common Flags

-r Recursive (delete directories)
-f Force (no prompts, ignore errors)
-i Interactive (ask before each delete)
rmdir Remove empty directories
Intermediate

Removes empty directories only. Safer than rm -r since it won't delete if there's content.

rmdir [directory]
Examples:
rmdir empty_folder # Remove empty dir
rmdir -p a/b/c # Remove nested empty dirs
👁️

Viewing Files

cat Display file contents
Essential

Outputs the entire contents of a file. Best for small files. Can also concatenate multiple files.

cat [options] [file(s)]
Examples:
cat file.txt # Display file
cat file1.txt file2.txt # Display multiple
cat -n file.txt # With line numbers
cat file.txt > new.txt # Copy via redirect

Common Flags

-n Number all lines
-b Number non-blank lines only
less View file with pagination
Essential

View files one screen at a time. Better than cat for large files. Navigate with arrow keys, search with /.

less [file]
Navigation inside less:
Space or f # Next page
b # Previous page
/pattern # Search forward
n # Next search result
g # Go to beginning
G # Go to end
q # Quit
head View first lines of file
Intermediate

Shows the first N lines of a file. Default is 10 lines.

head [options] [file]
Examples:
head file.txt # First 10 lines
head -n 20 file.txt # First 20 lines
head -5 file.txt # Shorthand for -n 5
tail View last lines of file
Intermediate

Shows the last N lines of a file. Great for logs! The -f flag watches for changes in real-time.

tail [options] [file]
Examples:
tail file.txt # Last 10 lines
tail -n 20 file.txt # Last 20 lines
tail -f server.log # Follow log in real-time!

Common Flags

-f Follow (watch for new content)
-n Number of lines to show
🔍

Search & Filter

grep Search text patterns in files
Essential

Searches for text patterns in files. Incredibly powerful for finding things. Supports regex.

grep [options] pattern [file(s)]
Examples:
grep "error" log.txt # Find "error" in file
grep -i "error" log.txt # Case insensitive
grep -r "TODO" . # Recursive search in dir
grep -n "function" *.js # Show line numbers
cat file | grep "pattern" # Pipe from another command

Common Flags

-i Case insensitive
-r Recursive (search subdirectories)
-n Show line numbers
-v Invert (show non-matching lines)
-c Count matches only
find Find files and directories
Essential

Searches for files and directories by name, type, size, date, and more. Very powerful.

find [path] [options] [expression]
Examples:
find . -name "*.js" # Find .js files
find . -type d -name "src" # Find directories named src
find . -type f -mtime -7 # Files modified in last 7 days
find . -size +100M # Files larger than 100MB
find . -name "*.log" -delete # Find and delete

Common Options

-name Match filename pattern
-type f Files only
-type d Directories only
-mtime Modified time (days)
-size File size
wc Word, line, character count
Intermediate

Counts lines, words, and characters in files. Great with pipes.

wc [options] [file]
Examples:
wc file.txt # lines, words, chars
wc -l file.txt # Line count only
ls | wc -l # Count files in directory
cat file.txt | wc -w # Word count

Common Flags

-l Lines only
-w Words only
-c Characters only
💻

System Info

whoami Current username
Essential

Shows the current logged-in username. Simple but handy.

whoami
Example:
$ whoami
joaquin
df Disk space usage
Intermediate

Shows disk space usage for mounted filesystems.

df [options]
Examples:
df # All filesystems
df -h # Human-readable (GB, MB)
df -h /home # Specific partition
du Directory size usage
Intermediate

Shows how much disk space directories and files use.

du [options] [path]
Examples:
du -sh * # Size of each item in current dir
du -h --max-depth=1 # One level deep
du -sh node_modules # Size of specific folder

Common Flags

-s Summary (total only)
-h Human-readable
top / htop Process monitor
Intermediate

Real-time view of running processes, CPU, and memory usage. htop is a prettier version.

top
htop
Inside top/htop:
q # Quit
k # Kill process
/ # Search (htop)
F6 # Sort by column (htop)
ps List processes
Intermediate

Shows running processes. Unlike top, it's a snapshot (not live).

ps [options]
Examples:
ps # Current terminal processes
ps aux # All processes, detailed
ps aux | grep node # Find node processes
kill Terminate processes
Intermediate

Sends signals to processes. Most commonly used to stop/kill them.

kill [signal] PID
Examples:
kill 1234 # Graceful terminate (SIGTERM)
kill -9 1234 # Force kill (SIGKILL)
killall node # Kill all node processes
history Command history
Essential

Shows previously executed commands. Use !number to re-run a command.

history [n]
Examples:
history # All history
history 20 # Last 20 commands
!123 # Re-run command #123
!! # Re-run last command
history | grep git # Find git commands
clear Clear terminal screen
Essential

Clears the terminal screen. Same as Ctrl+L shortcut.

clear
🔐

Permissions

chmod Change file permissions
Intermediate

Changes read (r), write (w), execute (x) permissions for owner, group, others.

chmod [mode] file
Examples:
chmod +x script.sh # Add execute permission
chmod 755 script.sh # rwxr-xr-x
chmod 644 file.txt # rw-r--r--
chmod -R 755 folder/ # Recursive

Common Modes

755 Owner: all, Others: read+execute (scripts)
644 Owner: read+write, Others: read (files)
600 Owner only (private files)
chown Change file owner
Advanced

Changes the owner and/or group of files. Usually requires sudo.

chown [user]:[group] file
Examples:
sudo chown joaquin file.txt # Change owner
sudo chown joaquin:staff file.txt # Owner and group
sudo chown -R joaquin folder/ # Recursive
sudo Run as superuser
Essential

Runs a command with administrator/root privileges. Use carefully!

sudo [command]
Examples:
sudo apt update # System update
sudo rm /etc/config # Delete system file
sudo -i # Interactive root shell
🌐

Networking

curl Transfer data from URLs
Essential

Transfers data to/from servers. Great for APIs, downloading files, testing endpoints.

curl [options] URL
Examples:
curl https://api.example.com # GET request
curl -o file.zip URL # Download file
curl -X POST -d '{"key":"val"}' URL # POST JSON
curl -I URL # Headers only

Common Flags

-o Output to file
-X HTTP method (POST, PUT, etc.)
-d Request body data
-H Add header
wget Download files
Intermediate

Downloads files from the web. Simpler than curl for basic downloads.

wget [options] URL
Examples:
wget https://example.com/file.zip # Download
wget -O custom.zip URL # Custom filename
wget -c URL # Resume interrupted download
ping Test network connectivity
Essential

Tests if a host is reachable. Shows response time.

ping [options] host
Examples:
ping google.com # Ping until Ctrl+C
ping -c 5 google.com # Ping 5 times
ssh Secure shell connection
Essential

Connects to remote servers securely. Essential for server management.

ssh [user@]host
Examples:
ssh user@192.168.1.1 # Connect to server
ssh -i key.pem user@host # With key file
ssh -p 2222 user@host # Custom port
scp Secure copy over SSH
Intermediate

Copies files between local and remote machines over SSH.

scp [source] [destination]
Examples:
scp file.txt user@host:/path/ # Upload
scp user@host:/path/file.txt . # Download
scp -r folder/ user@host:/path/ # Copy directory
📦

Package Managers

apt (Debian/Ubuntu) Linux package manager
Essential

Installs, updates, and removes software packages on Debian-based systems.

Examples:
sudo apt update # Update package list
sudo apt upgrade # Upgrade all packages
sudo apt install nginx # Install package
sudo apt remove nginx # Remove package
apt search keyword # Search packages
brew (macOS) macOS package manager
Essential

The missing package manager for macOS. Install almost anything.

Examples:
brew update # Update Homebrew
brew install node # Install package
brew upgrade # Upgrade all
brew list # List installed
brew search python # Search
npm Node.js package manager
Essential

Manages JavaScript/Node.js packages and dependencies.

Examples:
npm init # Initialize new project
npm install express # Install package
npm install # Install from package.json
npm install -g nodemon # Install globally
npm run start # Run script from package.json
npm update # Update packages
pip Python package manager
Essential

Installs Python packages from PyPI.

Examples:
pip install requests # Install package
pip install -r requirements.txt # From file
pip list # List installed
pip freeze > requirements.txt # Export deps
pip uninstall package # Remove
🌳

Git Version Control

git init Initialize repository
Essential

Creates a new Git repository in the current directory.

git init
git clone Clone a repository
Essential

Downloads a repository from a URL.

git clone [url]
Examples:
git clone https://github.com/user/repo.git
git clone git@github.com:user/repo.git # SSH
git status Check repository status
Essential

Shows which files are modified, staged, or untracked.

git status
git add Stage changes
Essential

Stages changes for the next commit.

git add [files]
Examples:
git add file.js # Stage specific file
git add . # Stage all changes
git add -A # Stage all including deletions
git commit Commit staged changes
Essential

Saves staged changes with a message.

git commit -m "message"
Examples:
git commit -m "Add login feature"
git commit -am "Quick fix" # Add + commit tracked files
git push Push to remote
Essential

Uploads local commits to a remote repository.

git push [remote] [branch]
Examples:
git push origin main
git push -u origin main # Set upstream
git push # If upstream is set
git pull Pull from remote
Essential

Downloads and merges changes from remote.

git pull [remote] [branch]
Examples:
git pull origin main
git pull # From tracked branch
git branch Manage branches
Intermediate

Lists, creates, or deletes branches.

git branch [options] [name]
Examples:
git branch # List branches
git branch feature # Create branch
git branch -d feature # Delete branch
git branch -a # List all (incl. remote)
git checkout / switch Switch branches
Intermediate

Switches between branches. git switch is the newer command.

Examples:
git checkout main # Switch to main
git checkout -b feature # Create and switch
git switch main # Modern syntax
git switch -c feature # Create and switch
git merge Merge branches
Intermediate

Merges another branch into your current branch.

git merge [branch]
Example:
git checkout main
git merge feature # Merge feature into main
git log View commit history
Intermediate

Shows commit history.

git log [options]
Examples:
git log # Full history
git log --oneline # Compact view
git log -5 # Last 5 commits
git log --graph # Visual branches
git stash Temporarily save changes
Intermediate

Saves uncommitted changes to a stack. Great for switching branches without committing.

Examples:
git stash # Save changes
git stash pop # Restore and remove from stash
git stash list # List stashes
git stash drop # Delete top stash
🔀

Redirection & Pipes

| (pipe) Send output to another command
Essential

Sends the output of one command as input to another. Chain commands together!

Examples:
ls -la | grep ".js" # Filter ls output
cat file.txt | wc -l # Count lines
ps aux | grep node # Find node processes
history | tail -20 # Last 20 commands
> and >> Redirect output to file
Essential

> overwrites file, >> appends to file.

Examples:
echo "Hello" > file.txt # Overwrite
echo "World" >> file.txt # Append
ls -la > listing.txt # Save ls output
cat file.txt >> backup.txt # Append file
< (input redirect) Use file as input
Intermediate

Uses a file as input for a command instead of typing.

Examples:
wc -l < file.txt # Count lines from file
sort < unsorted.txt > sorted.txt # Sort file
2> and &> Redirect errors
Advanced

2> redirects errors only. &> redirects both output and errors.

Examples:
command 2> errors.log # Errors to file
command &> all.log # All output to file
command 2>/dev/null # Discard errors
⚙️

Environment

echo Print text or variables
Essential

Outputs text to the terminal. Also used to display environment variables.

echo [text or $VARIABLE]
Examples:
echo "Hello World"
echo $HOME # Print home directory
echo $PATH # Print PATH variable
echo "Value: $VAR" # Variable in string
export Set environment variable
Intermediate

Sets environment variables for the current session and child processes.

export VAR=value
Examples:
export API_KEY="abc123"
export PATH="$PATH:/new/path" # Add to PATH
export NODE_ENV=production
env / printenv Show all environment variables
Intermediate

Lists all environment variables.

Examples:
env # List all
env | grep PATH # Find PATH
printenv HOME # Print specific variable
source / . Execute script in current shell
Intermediate

Runs a script in the current shell (not a subshell). Changes persist.

Examples:
source ~/.bashrc # Reload bash config
source .env # Load environment file
. ~/.zshrc # Shorthand for source
alias Create command shortcuts
Intermediate

Creates shortcuts for commands. Add to ~/.bashrc or ~/.zshrc to persist.

alias name='command'
Examples:
alias ll='ls -la'
alias gs='git status'
alias ..='cd ..'
alias python=python3