The terminal is your powerful command-line toolkit for managing files, running programs, and automating tasks on Linux, macOS, or Windows (with WSL or Git Bash). In just a few commands, you can replace repetitive menus, debug code, or backup files—all without clicking. Here’s how to start digging deep.
What You'll Need
- A computer running Linux/macOS, or Windows with WSL (Windows Subsystem for Linux) or Git Bash.
- Basic familiarity with keyboard shortcuts (Ctrl/Cmd + arrow keys, Tab for auto-completion).
- OpenTerminal (macOS/Linux) or Command Prompt/PowerShell (Windows).
- Optional: A text editor like VS Code or Nano for writing scripts.
Step-by-Step
- Open the Terminal:
- On macOS/Linux: Press
Cmd + Space, type "Terminal," and hit Enter. - On Windows: Open Command Prompt or PowerShell (search for "cmd" or "PowerShell").
Verify your OS version by running
uname -s(Linux/macOS) orsysteminfo(Windows). - On macOS/Linux: Press
- Navigate files efficiently:
- Move to your home directory:
cd ~. - Navigate to a folder:
cd Documentsorcd ~/Downloads.
Use
pwdto print your current directory. Need to jump to a specific folder? Usecd /path/to/folder(full path) orcd ../cd .(parent/child directories). - Move to your home directory:
- List files and folders:
- List all files:
ls(Linux/macOS) ordir(Windows). - Show hidden files (dotfiles):
ls -a(Linux/macOS) ordir /a(Windows). - Use
tree(Windows) ortree -a(Linux/macOS) to visualize folder structures. Install it viasudo apt install tree(Debian) orbrew install tree(macOS).
- List all files:
- Create and delete files/folders:
- Create a folder:
mkdir myfolder. - Delete a folder:
rmdir myfolder(empty) orrm -r myfolder(force, recursive). - Delete a file:
rm file.txt.
⚠️ Use
rm -ito confirm deletions before runningrm. - Create a folder:
- Copy, move, and rename:
- Copy a file:
cp file.txt backup.txt. - Move a file:
mv file.txt newfolder/. - Rename:
mv oldname.txt newname.txt.
- Copy a file:
- File permissions:
- Check permissions:
ls -l(Linux/macOS) ordir /ah(Windows). - Change permissions (Linux/macOS):
chmod 755 file.txt(owner:rwx, group:rx, others:rx).
Use
chownto alter file ownership (e.g.,sudo chown user:group file.txt). - Check permissions:
- Text file handling:
- View file contents:
cat file.txtortype file.txt(Windows). - Edit files: Use
nano file.txt,vim file.txt, or a GUI editor (e.g., VS Code). - Count lines/words:
wc file.txt.
- View file contents:
- Search files recursively:
- Find files by name:
find /path -name "file.txt"(Linux/macOS). - Search content:
grep "keyword" file.txt.
For Windows: Use
dir /s *keyword*.txtor PowerShell’sGet-ChildItem -Recurse -Filter "*.txt" | Select-String "keyword". - Find files by name:
- Run commands:
- Check installed packages (Linux):
apt list --installed(Debian) orbrew list(macOS). - Install a package:
sudo apt install nginx(Debian) orbrew install curl. - Update packages:
sudo apt update && sudo apt upgrade.
Windows: Use
choco install nginx(Chocolatey) orwinget install nginx. - Check installed packages (Linux):
- Automate with scripts:
- Create a script (e.g., backup.sh):
nano backup.sh, add commands, then save (Ctrl + X → Y → Enter). - Make it executable:
chmod +x backup.sh. - Run it:
./backup.sh.
For Windows: Use .bat files (e.g.,
echo Task1>task1.batto create a batch file). - Create a script (e.g., backup.sh):
- Networking basics:
- Check connected networks:
ip a(Linux/macOS) oripconfig(Windows). - Test connectivity:
ping google.com. - Forward a port (Linux):
sudo iptables -A FORWARD -p tcp --dport 80 -j ACCEPT.
- Check connected networks:
Common Mistakes
- Forgetting to use absolute paths: Always specify full paths (e.g.,
/home/user/Documents) when moving files across directories. - Ignoring hidden characters: Use
dos2unix(Linux/macOS) or Git’sgit config --global core.autocrlf inputto avoid line-ending issues. - Skipping permission checks: Run
sudoonly when necessary (it elevates privileges)—avoid it in scripts. - Mixing command-line and GUI folders: Terminal uses forward slashes (/); GUI uses backslashes (\).
Tips
- Master alias shortcuts: Add aliases to your shell config (e.g., Linux:
echo 'alias ll="ls -la"' >> ~/.bashrcto makelllist long. - Use history with
fc: Edit previous commands (e.g.,fc 5to edit command 5). - Tab completion: Press
Tabto auto-complete filenames or commands. - Debugging: Redirect error output with
command 2>&1 | tee error.log.
Troubleshooting
- Terminal doesn’t open:
- Restart the computer.
- Run
sudo service ssh start(Linux) or check Windows Task Manager for stuck processes. - If using WSL, ensure
wsl --shutdownis not in your startup settings.
- Commands not found:
- Verify the command exists:
which ls(Linux/macOS) orwhere ls(Windows). - Install missing tools (e.g.,
sudo apt install vim). - Check environment variables:
echo $PATH.
- Verify the command exists:
- Permission denied:
- Run with sudo:
sudo command. - Fix permissions:
chmod u+x script.sh. - For system files, ensure you’re root (e.g.,
sudo su).
- Run with sudo:
- Script fails at runtime:
- Check for typos:
grep "error" script.sh. - Add error handling:
if ! command; then echo "Error"; exit 1; fi. - Run with verbose output:
command -v verbose.
- Check for typos:
- Network issues:
- Restart your router.
- Verify DNS with
ping 8.8.8.8. - For port forwarding, test locally first.
Frequently Asked Questions
How do I switch between terminals on macOS/Linux?
Use multiple terminals (e.g., open Terminal 1, Terminal 2), or switch via Ctrl + Alt + T (most Linux distros). In macOS, hold Cmd while opening Terminal.
Can I use terminal commands on Windows without WSL?
Yes! Use Git Bash (comes with Git for Windows) or PowerShell with cross-platform tools like dir or set commands. For advanced Linux tools, WSL is recommended.
What’s the difference between ls -l and ls -la?
ls -l shows detailed info (permissions, owner, size) for visible files. ls -la adds hidden files (dotfiles) and lists them.
How do I clear the terminal in one command?
Linux/macOS: clear or printf "\033c". Windows: cls or cls + pause.
Can I search for files by extension only?
Yes: find /path -name "*.txt" (Linux/macOS) or dir /b /s *.md (Windows).
What’s the fastest way to create multiple folders?
Use mkdir -p folder1/folder2 (Linux/macOS) or mkdir folder1 folder2 (Windows). For bulk creation, combine with for i in {1..10}; do mkdir "Folder$i"; done.