# Common Linux Command-Line Tools (Portable Across Distros) 🧰

Below is a **practical, cross‑distro** “core toolbox” of commands you’ll use constantly. I’ll group them by job, give a **plain meaning**, and include **examples** you can try.

> Notes:
> 
> - Examples assume a typical Bash-like shell.
> - Many commands have lots of options—this is the “most useful baseline.”

---

## 1) Getting oriented (where am I? what’s here?)

### `pwd` — print working directory

Shows your current directory.

```bash
pwd
# /home/alex/projects
```

### `ls` — list directory contents

```bash
ls
ls -l        # long listing (permissions, owner, size, time)
ls -a        # include hidden files (dotfiles)
ls -lah      # long + all + human-readable sizes
```

### `cd` — change directory

```bash
cd /etc
cd ..        # up one directory
cd ~         # home directory
cd -         # previous directory
```

### `tree` — show directory tree (often needs installing)

```bash
tree
tree -L 2    # limit depth
```

---

## 2) Creating, moving, copying, deleting (file operations)

### `touch` — create an empty file / update timestamp

```bash
touch notes.txt
```

### `mkdir` — make directories

```bash
mkdir logs
mkdir -p a/b/c   # create parents as needed
```

### `cp` — copy files/directories

```bash
cp a.txt b.txt
cp -r src/ backup-src/     # copy directory recursively
cp -a src/ backup-src/     # archive mode: preserve permissions/times (very common)
```

### `mv` — move/rename files/directories

```bash
mv oldname.txt newname.txt
mv file.txt /tmp/
```

### `rm` — remove files/directories (destructive)

```bash
rm file.txt
rm -r folder/       # recursive delete directory
rm -rf folder/      # force + recursive (use extreme caution)
```

### `ln` — create links (symlinks are very common)

```bash
ln -s /var/www/site/current/public public   # symlink
```

---

## 3) Reading files quickly

### `cat` — print file contents

Good for small files; for big ones prefer `less`.

```bash
cat /etc/hostname
```

### `less` — page through text interactively

Keys: `q` quit, `/text` search, `n` next match.

```bash
less /var/log/syslog
```

### `head` / `tail` — show start/end of file

```bash
head -n 20 access.log
tail -n 50 error.log
tail -f error.log     # follow appended log lines (real-time)
```

### `nl` — show file with line numbers

```bash
nl -ba nginx.conf
```

---

## 4) Help &amp; discovery

### `man` — manual pages

```bash
man ls
man -k network    # search man page descriptions (keyword)
```

### `--help` — quick built-in help (common convention)

```bash
grep --help
```

### `which` / `type` — where a command comes from

```bash
which python
type ls            # also tells if it’s an alias/function/builtin
```

---

## 5) Searching text (the everyday “find inside files” tools)

### `grep` — search text for patterns

```bash
grep "listen" nginx.conf
grep -R "DB_HOST" .           # recursive search
grep -n "error" app.log        # include line numbers
grep -i "warning" app.log      # case-insensitive
```

### `sed` — stream editor (common for simple substitutions)

```bash
sed 's/http:/https:/g' urls.txt
sed -n '1,20p' file.txt        # print specific lines
```

<details id="bkmrk-sed-%F0%9F%A4%96-sed-is-a-strea"><summary>sed</summary>

🤖 `sed` is a **stream editor**: it reads text **one line at a time**, applies one or more editing “commands” you give it, and then (by default) **prints the result**. It’s especially useful in pipelines because it can transform text *as it flows through*—without you opening an editor. 🛠️

1. **How** `sed` **thinks (the mental model)**
    
    
    1. `sed` takes input from:
        
        
        - a file: `sed '...' file.txt`
        - *or* stdin (a pipe): `some_command | sed '...'`
    2. For each line, it:
        
        
        - loads the line into a temporary buffer (often called the *pattern space*)
        - runs your commands on that line
        - prints the line (unless you tell it not to)
2. **The most common command: substitution (**`s/…/…/`**)**  
    The form is:
    
    ```bash
    sed 's/PATTERN/REPLACEMENT/FLAGS'
    ```
    
    Example from your notes:
    
    ```bash
    sed 's/http:/https:/g' urls.txt
    ```
    
    What it means:
    
    
    - `s` = “substitute”
    - `http:` = the text (or regex) to find
    - `https:` = what to replace it with
    - `g` = “global” on the line (replace **all** matches in that line, not just the first)
    
    Concrete example:
    
    
    - Input line: [`http://a`](http://a)` `[`http://b`](http://b)
    - Without `g` (`sed 's/http:/https:/'`): [`https://a`](https://a)` `[`http://b`](http://b)
    - With `g` (`sed 's/http:/https:/g'`): [`https://a`](https://a)` `[`https://b`](https://b)
3. **Patterns are usually regular expressions (regex)**  
    `sed` patterns are typically regex, so you can match more than literal text.
    
    
    - Replace any run of spaces with one space:
        
        ```bash
        echo "a     b" | sed 's/  */ /g'
        ```
    - Replace only at the start of the line using `^`:
        
        ```bash
        echo "ERROR: bad" | sed 's/^ERROR:/WARN:/'
        ```
    - Replace only at the end using `$`:
        
        ```bash
        echo "file.tmp" | sed 's/\.tmp$/.txt/'
        ```
    
    Notes:
    
    
    - In basic `sed`, some regex operators need escaping (like `\+` instead of `+`). On many systems you can use extended regex with `-E`:
        
        ```bash
        echo "a     b" | sed -E 's/ +/ /g'
        ```
4. **Printing vs suppressing output (**`-n` **and** `p`**)**  
    By default, `sed` prints every (possibly modified) line. If you only want *some* lines, use `-n` (no auto-print) plus `p` (print).
    
    Your example:
    
    ```bash
    sed -n '1,20p' file.txt
    ```
    
    Meaning:
    
    
    - `-n` = don’t print everything automatically
    - `1,20p` = print only lines 1 through 20
    
    Similarly, to print only lines that match a pattern:
    
    ```bash
    sed -n '/error/p' app.log
    ```
5. **Filtering/deleting lines (**`d`**)**  
    Another very common use is deleting lines (i.e., filtering them out).
    
    
    - Delete blank lines:
        
        ```bash
        sed '/^$/d' file.txt
        ```
    - Delete comment lines starting with `#`:
        
        ```bash
        sed '/^#/d' file.txt
        ```
6. **In-place editing (**`-i`**)—powerful but easy to misuse**  
    If you want to actually modify a file on disk (instead of just printing to the terminal), you can use `-i`.
    
    
    - Replace in a file *in place*:
        
        ```bash
        sed -i 's/old/new/g' config.txt
        ```
    
    Many people prefer making a backup:
    
    ```bash
    sed -i.bak 's/old/new/g' config.txt
    ```
    
    That will leave `config.txt.bak` behind as a safety net.
7. **Putting it in pipelines (why it’s called a “stream” editor)**  
    `sed` shines when transforming command output:
    
    ```bash
    ps aux | sed -n '1,5p'
    ```
    
    Or when cleaning data before feeding it to something else:
    
    ```bash
    cat access.log | sed 's/"//g' | cut -d' ' -f1 | sort | uniq -c | sort -nr
    ```

If you tell me what exactly felt unclear (e.g., the `s///g` syntax, regex characters, or `-n ... p`), I can tailor a few examples directly to your kind of files/logs.

</details>### `awk` — field/column processing

```bash
awk '{print $1, $9}' access.log   # e.g., IP and status code columns (depends on log format)
```

---

## 6) Counting, sorting, unique-ing (text pipelines)

### `wc` — count lines/words/bytes

```bash
wc -l access.log     # number of lines
```

### `sort` — sort lines

```bash
sort names.txt
sort -n numbers.txt  # numeric sort
```

### `uniq` — collapse adjacent duplicates (usually used after `sort`)

```bash
sort users.txt | uniq
sort users.txt | uniq -c | sort -nr   # frequency count, descending
```

### `cut` — extract columns/fields

```bash
cut -d: -f1 /etc/passwd     # usernames (field 1, delimiter ':')
```

### `tr` — translate/delete characters

```bash
tr '[:lower:]' '[:upper:]' < file.txt
```

---

## 7) Composing commands (pipes, redirection, and “do this to each line”)

### `|` (pipe) — send output of one command into another

```bash
ps aux | grep nginx
```

### Redirection: `>`, `>>`, `2>`, `&>`

```bash
echo "hello" > out.txt      # overwrite
echo "more"  >> out.txt     # append
cmd 2> err.txt              # stderr to file
cmd &> all.txt              # stdout+stderr to file (bash)
```

### `tee` — write output to file *and* keep showing it

```bash
echo "config" | tee -a notes.txt
```

### `xargs` — turn input lines into arguments (batch operations)

```bash
find . -name "*.log" | xargs rm -f
# safer form when filenames may contain spaces:
find . -name "*.log" -print0 | xargs -0 rm -f
```

---

## 8) Finding files

### `find` — search by name/type/time/size (very common)

```bash
find . -name "*.conf"
find /var/log -type f -mtime -7     # files modified in last 7 days
find . -type f -size +100M          # larger than 100MB
```

### `locate` — fast filename search via database (needs updated index)

```bash
locate nginx.conf
```

---

## 9) Permissions &amp; ownership (you’ll use these constantly on servers)

### `chmod` — change permissions

```bash
chmod 644 file.txt    # rw-r--r--
chmod 755 script.sh   # rwxr-xr-x
chmod -R 755 public/  # recursively (use carefully)
```

### `chown` / `chgrp` — change owner / group

```bash
chown www-data:www-data -R /var/www/site
chgrp developers project/
```

### `umask` — default permission mask for new files

```bash
umask
```

---

## 10) Processes &amp; system state (baseline diagnostics)

### `ps` — show processes

```bash
ps aux
ps aux | grep php-fpm
```

### `top` / `htop` — live process viewer (`htop` may need install)

```bash
top
htop
```

### `kill` / `pkill` — send signals to processes

```bash
kill 1234
pkill nginx
kill -TERM 1234   # graceful request
kill -KILL 1234   # force (last resort)
```

### `systemctl` — manage services (systemd systems)

```bash
systemctl status nginx
systemctl restart nginx
systemctl enable nginx   # start on boot
```

### `journalctl` — systemd logs

```bash
journalctl -u nginx
journalctl -u nginx -f       # follow
journalctl -b                # logs since boot
```

---

## 11) Networking essentials (developer/server workflow)

### `ip` — network interfaces and routes

```bash
ip a          # addresses
ip r          # routes
```

### `ss` — see listening ports and connections

```bash
ss -tulpn     # TCP/UDP listening + process names
```

### `ping` — basic reachability test

```bash
ping -c 4 example.com
```

### `curl` — make HTTP requests (huge for web debugging)

```bash
curl https://example.com
curl -I https://example.com          # headers only
curl -v https://example.com          # verbose (TLS + connection details)
```

### `dig` / `nslookup` — DNS queries (often `dig`)

```bash
dig example.com A
dig example.com +short
```

---

## 12) Archives &amp; compression (moving code, backups)

### `tar` — create/extract tar archives (the Linux standard)

```bash
tar -czf site.tar.gz site/     # create gzip-compressed archive
tar -xzf site.tar.gz           # extract
```

### `gzip` / `xz` — compression tools (often used via `tar`)

```bash
gzip large.log
xz -T0 bigfile     # stronger compression; -T0 uses all cores
```

### `zip` / `unzip` — common for cross-platform bundles

```bash
zip -r project.zip project/
unzip project.zip
```

---

## 13) Remote access &amp; file transfer (core server skills)

### `ssh` — remote shell

```bash
ssh user@server.example.com
ssh -i ~/.ssh/id_ed25519 user@server
```

### `scp` — copy over SSH (simple, not always the best)

```bash
scp file.txt user@server:/tmp/
scp -r site/ user@server:/var/www/
```

### `sftp` — interactive file transfer over SSH

```bash
sftp user@server
# then: put/get, ls, cd, etc.
```

---

## 14) Privilege escalation (admin work)

### `sudo` — run as root (or another user)

```bash
sudo apt update
sudo systemctl restart nginx
```

---

## 15) “Environment” and shell basics you’ll constantly touch

### `echo` — print text / variables

```bash
echo "Hello"
echo "$HOME"
```

### `env` / `printenv` — show environment variables

```bash
env
printenv PATH
```

### `export` — set environment variable for this shell session

```bash
export NODE_ENV=production
```

### `history` — show command history

```bash
history | tail
```

---

### If you want, I can turn this into a printable “cheat sheet”

Tell me your target context: **web server admin (WordPress)**, **Node/React dev**, or **general Linux**—and whether you’re on **Debian-like** or **RHEL-like** most often.