The terminal is a powerful tool for automation, system management, and troubleshooting—once you know the right commands. Type `echo "Hello World"` into the terminal and press Enter to see it work instantly. For deeper control, use core commands like `ls`, `cd`, and `pwd` to navigate and inspect files, or `sudo` to run privileged actions. This guide covers essentials for Linux/macOS (Bash/Zsh) and Windows (PowerShell/Cmd).
What You'll Need
- A working terminal emulator:
- Linux:
gnome-terminal,konsole, orxterm(preinstalled) - macOS:
Terminal.app(default) oriTerm2(recommended) - Windows:
PowerShell(default) orWindows Terminal(recommended for advanced users)
- Linux:
- Basic familiarity with file paths (e.g.,
/home/useron Linux/macOS,C:\Users\useron Windows) - Administration rights (for commands requiring
sudoor elevated permissions)
Step-by-Step: Core Terminal Commands
-
Open Terminal:
- Linux/macOS: Press Ctrl + Alt + T or search for "Terminal" in the app launcher.
- Windows (PowerShell): Press Win + X then select "Windows Terminal" or "PowerShell".
- Windows (Command Prompt): Press Win + R, type
cmd, then press Enter.
-
Check your current directory:
- Linux/macOS: Run
pwd(Print Working Directory). Example output:/home/user/Documents. - Windows (PowerShell): Run
Get-Location. Example output:C:\Users\user\Documents.
- Linux/macOS: Run
-
List files/folders:
- Linux/macOS: Run
ls(long list:ls -l). Add-ato show hidden files (e.g.,ls -la). - Windows (PowerShell): Run
Get-ChildItemor aliasdir. Add-Forceto show hidden files (e.g.,dir -Force).
- Linux/macOS: Run
-
Navigate directories:
- Go up one level:
cd .. - Go to root:
cd /(Linux/macOS) orcd \(Windows). - Go to a specific folder:
cd path/to/folder. Example:cd Downloads.
- Go up one level:
-
Create/delete files/folders:
- Create a folder:
mkdir foldername(Linux/macOS) orNew-Item -ItemType Directory -Name foldername(PowerShell). - Delete a folder (recursively):
rm -r foldername(Linux/macOS) orRemove-Item -Recurse foldername(PowerShell). - Warning: Commands like
rm -rforRemove-Item -Recurse -Forcepermanently delete content. Double-check paths!
- Create a folder:
-
View file contents:
- Linux/macOS:
cat filename(whole file) orless filename(scrollable). - Windows (PowerShell):
Get-Content filenameortype filename(legacy).
- Linux/macOS:
-
Search for files:
- Linux/macOS:
find /path -name "filename". Example:find ~/Documents -name "report.txt". - Windows (PowerShell):
Get-ChildItem -Recurse -Filter "filename" -Path "C:\path".
- Linux/macOS:
-
Manage permissions (Linux/macOS only):
- Check permissions:
ls -l filename(e.g.,-rw-r--r--). - Change ownership:
sudo chown user:group filename. - Change permissions:
chmod 755 filename(read/write/execute for owner, read/execute for others).
- Check permissions:
-
Run commands as administrator (Linux/macOS/Windows):
- Linux/macOS: Prepend with
sudo. Example:sudo apt update. You’ll be prompted for your password. - Windows (PowerShell/Cmd): Open Terminal as admin (right-click > "Run as administrator") or use
Start-Process cmd -Verb RunAs(PowerShell).
- Linux/macOS: Prepend with
-
Chain commands (pipelines):
- Linux/macOS: Use
|to pass output between commands. Example:ls -l | grep ".txt"Lists only text files. - Windows (PowerShell): Use
|similarly. Example:Get-ChildItem | Where-Object { $_.Extension -eq ".txt" }
- Linux/macOS: Use
-
Automation with scripts:
- Create a shell script: Open a text editor (e.g.,
nano script.sh), add commands like#!/bin/bash\necho "Hello", save, then make it executable withchmod +x script.sh. Run it with./script.sh. - Windows (PowerShell): Create a file
script.ps1with commands likeWrite-Host "Hello", then run it with.\script.ps1.
- Create a shell script: Open a text editor (e.g.,
Common Mistakes
-
Forgetting paths: Commands fail if you don’t specify full paths. Example:
rm file.txtdeletes from the current directory, butrm /path/to/file.txttargets that exact location. -
Mistyping commands: The terminal is case-sensitive (e.g.,
Ls≠ls). Use tab completion (press Tab) to auto-fill paths or commands. -
Ignoring warnings: Commands like
sudoorrm -rfcan cause irreversible damage. Always verify paths and confirm actions. -
Assuming cross-platform commands work identically:
dir(Windows) ≠ls(Linux/macOS). Use platform-specific commands or aliases.
Tips for Efficiency
-
Customize your prompt:
- Linux/macOS: Edit
~/.bashrcor~/.zshrcto add:PS1='\[\e[32m\]\u@\h\[\e[0m\]:\[\e[34m\]\w\[\e[0m]\$ 'Then reload withsource ~/.bashrc. - Windows (PowerShell): Use
Set-PSReadlineOption -Prompt "(PS) \w > "in a new session.
- Linux/macOS: Edit
-
Use aliases for frequent commands:
Then reload the file withLinux/macOS (add to ~/.bashrc or ~/.zshrc)
alias ll='ls -la' alias update='sudo apt update && sudo apt upgrade'source ~/.bashrc. - Leverage history: Press ↑/↓ to scroll through past commands. Edit and rerun with Enter.
- Search history: Press Ctrl + R to reverse-search commands by typing keywords.
-
Open files directly from the terminal:
- Linux/macOS:
code filename(opens with VS Code) ornano filename. - Windows:
start filename(opens with default app) orcode filename(if VS Code is installed).
- Linux/macOS:
Troubleshooting
Command Not Found
If you see "command not found", try these steps:
- Check spelling and syntax (e.g.,
lsvsLs). - Verify the command exists on your platform. Example:
dir(Windows) ≠ls(Linux/macOS). - Install missing tools:
- Linux (Debian/Ubuntu):
sudo apt install package-name. - macOS: Use
brew install package-name(Homebrew) or download from the manufacturer. - Windows: Use
winget install package-name(Windows Package Manager) or download from Microsoft Store. - Check if the command is in your
$PATH. Runecho $PATH(Linux/macOS) or$env:PATH(PowerShell) to see available directories. - If using PowerShell, ensure it’s the correct version (some legacy commands require
cmd.exe).
Permission Denied
If you get "Permission denied", try:
- Run the command with
sudo(Linux/macOS) or as administrator (Windows). - Change file permissions (Linux/macOS only):
chmod +x filenameto make it executable. - Check ownership:
ls -l filenameto see who owns the file. If it’s yours, usesudo chown $USER filename.
Terminal Crashes or Freezes
- Restart the terminal: Close and reopen the app.
- Update your terminal emulator or OS.
- Check for conflicting scripts or commands. Run simpler commands to isolate the issue.
- On Linux/macOS, reset the terminal configuration with
reset. - If using PowerShell, check for corrupted profiles with
Remove-Item -Force $PROFILE(backup first).
Commands Don’t Do What You Expect
- Review the command’s manual page:
man command-name(Linux/macOS) orGet-Help command-name -Full(PowerShell). - Test with sample data first. Example:
echo "test" | grep "pattern". - Check for typos in arguments. Example:
ls -l≠ls -a.
If That Did Not Work
For persistent issues:
- Reinstall the terminal emulator (backup configurations first).
- On Linux, check for missing dependencies with
sudo apt --fix-broken install. - For PowerShell, reset to default settings with
Reset-ConsoleHost. - If the OS itself is misbehaving, consider creating a system restore point (Windows) or reinstalling the OS (last resort).
Frequently Asked Questions
How do I exit the terminal?
Type exit or press Ctrl + D (Linux/macOS terminal). On Windows, exit works in both PowerShell and Command Prompt.
Can I use the terminal on a remote server?
Yes! Use ssh user@remote-server-ip (Linux/macOS/Windows PowerShell) to connect. Replace user with your username and remote-server-ip with the server’s IP address. Example:
ssh john@192.168.1.100You’ll be prompted for a password or SSH key.
How do I redirect output to a file?
Use these symbols:
> filename: Overwrite a file with output.>> filename: Append to a file (Linux/macOS/Windows PowerShell).2> filename: Redirect errors to a file.&> filename(Linux/macOS) or*> filename(PowerShell): Redirect both output and errors.
Linux/macOS
ls /nonexistent >> error.log 2>&1PowerShell
Get-ChildItem nonexistent | Out-File -FilePath error.log -Append
Why does my terminal keep asking for my password?
This typically happens when:
- You use
sudowithout proper permissions. - Your shell configuration (e.g.,
~/.bashrc) has commands that require sudo. - You’re running a script that needs elevated privileges.
Solution: Check your scripts and configurations for sudo calls. If you’re not sure, run the terminal as a non-root user and test commands one by one.
How do I find all files with a specific extension?
Use these commands:
- Linux/macOS:
find /path -type f -name "*.txt" - Windows (PowerShell):
Get-ChildItem -Path "C:\path" -Recurse -Filter "*.txt"
*.txt with your file extension (e.g., *.pdf).
Is it safe to use rm -rf / or Remove-Item -Recurse -Force C:\?
No. These commands permanently delete all files and folders on your system, including the operating system itself. Never run them unless you’re absolutely certain of the target path and are prepared for data loss. If you accidentally trigger this, you may need to reinstall your OS.