98 lines
3.5 KiB
Markdown
98 lines
3.5 KiB
Markdown
# WSL / Ubuntu Command-Line Cheat Sheet
|
|
|
|
## Filesystem Navigation
|
|
|
|
| Command | What it does | Example |
|
|
|---------|-------------|---------|
|
|
| `pwd` | Print the current directory (where you are) | `pwd` → `/home/alice` |
|
|
| `ls` | List files and folders | `ls`, `ls -la` (detailed view) |
|
|
| `cd <dir>` | Change directory | `cd Documents`, `cd ~` (home), `cd ..` (up one) |
|
|
| `mkdir <name>` | Create a new directory | `mkdir projects` |
|
|
| `rmdir <dir>` | Remove an **empty** directory | `rmdir old_folder` |
|
|
|
|
## File Operations
|
|
|
|
| Command | What it does | Example |
|
|
|---------|-------------|---------|
|
|
| `touch <file>` | Create an empty file | `touch notes.txt` |
|
|
| `cp <src> <dst>` | Copy a file | `cp notes.txt backup.txt` |
|
|
| `mv <src> <dst>` | Move or rename a file | `mv notes.txt ~/Documents/` |
|
|
| `rm <file>` | Delete a file (⚠️ no trash bin) | `rm temp.txt` |
|
|
| `rm -r <dir>` | Delete a folder and **everything inside** | `rm -r old_project/` |
|
|
| `cat <file>` | Print a file's contents to the terminal | `cat notes.txt` |
|
|
| `less <file>` | View a file page-by-page (press `q` to quit) | `less long_file.txt` |
|
|
| `head <file>` | Show the first 10 lines | `head data.csv` |
|
|
| `tail <file>` | Show the last 10 lines | `tail -n 20 data.csv` |
|
|
| `nano <file>` | Edit a file in the terminal | `nano notes.txt` |
|
|
|
|
## Viewing & Finding
|
|
|
|
| Command | What it does | Example |
|
|
|---------|-------------|---------|
|
|
| `file <name>` | Show what type of file something is | `file notes.txt` |
|
|
| `find <dir> -name <pattern>` | Find files by name | `find ~ -name "*.md"` |
|
|
| `grep <pattern> <file>` | Search inside files | `grep "error" log.txt` |
|
|
|
|
## Permissions
|
|
|
|
| Command | What it does | Example |
|
|
|---------|-------------|---------|
|
|
| `chmod <mode> <file>` | Change file permissions | `chmod +x script.sh` (make executable) |
|
|
| `sudo <command>` | Run a command as admin (root) | `sudo apt update` |
|
|
|
|
## Package Management (apt)
|
|
|
|
| Command | What it does | Example |
|
|
|---------|-------------|---------|
|
|
| `sudo apt update` | Refresh the list of available packages | Run before installing anything |
|
|
| `sudo apt install <pkg>` | Install a package | `sudo apt install clang` |
|
|
| `sudo apt upgrade` | Upgrade all installed packages | `sudo apt upgrade` |
|
|
| `sudo apt remove <pkg>` | Uninstall a package | `sudo apt remove clang` |
|
|
| `apt search <keyword>` | Search for a package | `apt search image editor` |
|
|
|
|
## Pipes & Redirection
|
|
|
|
| Command | What it does | Example |
|
|
|---------|-------------|---------|
|
|
| `cmd1 \| cmd2` | Send output of `cmd1` into `cmd2` | `ls \| grep ".txt"` |
|
|
| `>` | Redirect output to a file (overwrite) | `echo "hello" > hello.txt` |
|
|
| `>>` | Redirect output to a file (append) | `echo "world" >> hello.txt` |
|
|
|
|
## WSL-Specific Tips
|
|
|
|
- Your **Windows files** are under `/mnt/c/` — e.g. `/mnt/c/Users/YourName/Documents`
|
|
- Your **Linux home** is at `/home/yourname` (type `~` as a shortcut)
|
|
- Drag a file from Windows Explorer into the terminal to paste its WSL path
|
|
- Use `explorer.exe .` to open the current Linux folder in Windows File Explorer
|
|
|
|
## Useful Shortcuts
|
|
|
|
| Shortcut | What it does |
|
|
|----------|-------------|
|
|
| `Ctrl + C` | Cancel the current command |
|
|
| `Ctrl + D` | Exit the terminal / close session |
|
|
| `Tab` | Auto-complete file and folder names |
|
|
| `↑` / `↓` | Scroll through command history |
|
|
| `Ctrl + L` | Clear the screen |
|
|
|
|
## Quick Reference — First Steps
|
|
|
|
```bash
|
|
# Where am I?
|
|
pwd
|
|
|
|
# What's in this folder?
|
|
ls -la
|
|
|
|
# Make a project folder and go inside
|
|
mkdir my_project
|
|
cd my_project
|
|
|
|
# Create a file and edit it
|
|
touch hello.txt
|
|
nano hello.txt
|
|
|
|
# Come back home
|
|
cd ~
|
|
```
|