10 Linux Terminal Tricks Every Ethical Hacker Should Know

 Stop retyping commands. Start moving faster than the clock.

Image: AI Generated

If you're learning cybersecurity, penetration testing, or system administration, you already know the big names: Nmap, Wireshark, Burp Suite, Metasploit, tcpdump. But here's what nobody tells you early enough — the terminal itself is a tool, and mastering it is just as important as mastering any scanner.

These ten shortcuts and shell tricks won't make you a hacker overnight. What they will do is shave hours off every assessment, investigation, and CTF you touch. They're the difference between fighting your keyboard and flowing through it.

A quick word on ethics: everything below is for authorized security testing, CTFs, lab environments, and your own machines. Never point these at systems you don't own or have written permission to test. That's not a disclaimer to skim — it's the line between a professional and a problem.

Grab a VM (Kali, Parrot, or any Linux box), and let's go.

1. Ctrl + R — Never Lose a Command Again

Yesterday you ran a monster Nmap scan. Today you need it again, but it's buried under 200 commands of ls and cd.

Press Ctrl + R, type a fragment, and the shell finds it for you:

text
(reverse-i-search)`nmap': nmap -sV -sC -Pn -p- --min-rate 2000 192.168.1.0/24 -oA lab_scan

Here's the pro move: don't just hit Enter. Press the Right Arrow to pull the command onto your normal prompt, edit it — change the subnet, tweak the ports — and then run it. You just reused a 70-character command and adapted it in three seconds.

  • Ctrl + R again → cycle through older matches

  • Ctrl + G → cancel the search, keep your line

  • → → pull the match to your prompt for editing

Why it matters: during engagements, your best one-liners get refined instead of retyped — and retyping is where typos and wrong targets happen.

2. sudo !! — Fix "Permission Denied" in Two Keystrokes

You type a command. Enter. And get:

text
$ systemctl restart sshd
Failed to restart sshd.service: Interactive authentication required.

Don't retype it. Just run:

bash
sudo !!

The shell expands !! into your entire previous command:

bash
sudo systemctl restart sshd

A few history expansions worth memorizing:

bash
!!          # the whole previous command
sudo !!     # previous command + sudo
!$          # the last ARGUMENT of the previous command
!nmap       # the most recent command starting with "nmap"
!42         # command #42 from `history`

That !$ one is quietly brilliant. Watch:

bash
mkdir /opt/AntiGravity
cd !$                    # → cd /opt/AntiGravity

Why it matters: you stay in flow. No more "up-arrow, Home key, type sudo, Enter" — especially mid-investigation when you're restarting services or reading protected configs.


3. Alt + . — The Last Argument, One Keystroke Away

This is the most powerful shortcut almost nobody knows.

You download a wordlist:

bash
wget https://example.com/wordlists/raft-medium-directories.txt

Now you want to use that exact path in your next tool. Type wc -l, press Alt + ., and:

bash
$ wc -l raft-medium-directories.txt

A real recon chain:

bash
wget https://github.com/danielmiessler/SecLists/raw/master/Discovery/Web-Content/common.txt
gobuster dir -u http://10.10.10.5 -w    # press Alt + . here

Result:

bash
gobuster dir -u http://10.10.10.5 -w common.txt

Bonus: press Alt + . repeatedly to walk backwards through the last arguments of previous commands — not just the most recent one.

Why it matters: long filenames, PCAP paths, payload names, and log locations become one keystroke away. This is the single biggest time-saver when chaining tools.


4. Edit Commands Without the Arrow Keys

You typed this and hit Enter too fast:

bash
nmap -sV -Pn -p 80,443,8080 target.htb

You forgot sudo. Your instinct is to hold the Left Arrow for thirty seconds. Don't.

KeyAction
Ctrl + AJump to start of line
Ctrl + EJump to end of line
Ctrl + WDelete the previous word
Ctrl + KDelete from cursor to end
Alt + B / Alt + FMove word by word

The fix:

  1. Press ↑ to recall the command

  2. Ctrl + A → cursor jumps to the start

  3. Type sudo → done

  4. Ctrl + E → back to the end, hit Enter

Another use — Ctrl + W in action:

Before:

bash
nano /etc/apache2/sites-enabled/example.conf

Press Ctrl + W to delete the filename:

After:

bash
nano /etc/apache2/sites-enabled/

Type the new filename. Done.


5. tee — Writing to Protected Files the Right Way

This is a classic trap that trips up every beginner.

You need to add a lab host entry:

bash
echo "10.10.14.5 target.htb" >> /etc/hosts
text
bash: /etc/hosts: Permission denied

So you try:

bash
sudo echo "10.10.14.5 target.htb" >> /etc/hosts

It still fails. Here's why:

The shell handles the >> redirection before sudo runs. The shell — running as your user — is the one trying to open /etc/hosts for writing. sudo only elevates echo, which does nothing useful.

The correct way:

bash
echo "10.10.14.5 target.htb" | sudo tee -a /etc/hosts
FormEffect
... | sudo tee fileOverwrite file
... | sudo tee -a fileAppend to file
... | sudo tee file > /dev/nullWrite without screen echo

Real example — hardening SSH in a lab:

bash
echo "PermitRootLogin no" | sudo tee -a /etc/ssh/sshd_config
sudo sshd -t && sudo systemctl restart sshd   # always validate first!

Why it matters: you can edit /etc/hosts, /etc/resolv.conf, /etc/sysctl.conf, and sshd_config reliably — and you now understand why the naive approach fails. That's a classic interview question and a very real pitfall.


6. Brace Expansion — Generate Anything Instantly

Setting up a CTF workspace? Brace expansion is your new best friend.

bash
echo {1..5}                 # 1 2 3 4 5
echo {a..e}                 # a b c d e
echo {1..100..10}           # 1 11 21 31 ... 91
touch file{1..5}.txt        # file1.txt ... file5.txt

A real workspace in one line:

bash
mkdir -p pentestlab/{recon,exploit,loot,notes}
touch pentestlab/recon/scan{1..3}.nmap
touch pentestlab/loot/cred{1..5}.txt
tree pentestlab

Output:

text
pentestlab
├── exploit
├── loot
│   ├── cred1.txt ... cred5.txt
├── notes
└── recon
    ├── scan1.nmap ... scan3.nmap

The backup trick you'll use forever:

bash
cp /etc/ssh/sshd_config{,.bak}
# expands to: cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak

7. Delete Multiple File Types — Safely

Your evidence folder is drowning in screenshots, PDFs, and random JPGs.

bash
rm *.{png,jpg,pdf}

But always preview first:

bash
# 1. PREVIEW
ls *.{png,jpg,pdf}

# 2. If the list looks right, delete
rm *.{png,jpg,pdf}

# 3. Even safer — interactive
rm -i *.{png,jpg,pdf}

⚠️ rm has no undo and no recycle bin. On an engagement, a misplaced glob can destroy your evidence. Preview with ls every single time.


8. Cut and Paste on the Command Line

You're mid-way through a capture command:

bash
tcpdump -i eth0 host 192.168.1.10 and port 443 -w capture.pcap

…but you suddenly can't remember which interface is which. You don't want to lose the command.

  • Ctrl + U — cut everything before the cursor (saved to a buffer)

  • Ctrl + Y — paste it back

bash
# Type the long command, then:
^U                          # line vanishes, but is stored
ip a                        # check your interfaces
^Y                          # the whole command returns

Why it matters: you can "park" a command, run a quick check, and bring it back untouched. Perfect for long tcpdump, nmap, and ffuf invocations where one wrong interface means starting over.


9. Ctrl + Z — Suspend a Process Without Killing It

You start a capture:

bash
sudo tcpdump -i eth0

…and realize you need your shell back, but you don't want to kill the capture.

Key/CommandAction
Ctrl + ZSuspend (pause) the running process
jobsList suspended/background jobs
fgResume most recent job in foreground
bgResume most recent job in background
fg %2 / bg %1Target a specific job
disownDetach so it survives shell exit
bash
$ sudo tcpdump -i eth0
^Z
[1]+  Stopped                 sudo tcpdump -i eth0

$ jobs
[1]+  Stopped                 sudo tcpdump -i eth0

$ ls -la ~/captures        # quick task

$ fg
# capture resumes

Better still — background it from the start:

bash
sudo tcpdump -i eth0 -w /tmp/cap.pcap &
jobs

Why it matters: run tcpdump, nc -lvnp 4444 listeners, Python servers, and long scripts alongside your shell work — no extra terminal tabs, no accidental Ctrl + C.


10. alias — Build Your Own Command Shortcuts

You type the same 40-character commands every day. Stop it.

bash
alias ll='ls -lah'

Now ll runs ls -lah. To make it permanent, open ~/.bashrc:

bash
nano ~/.bashrc

Add a block:

bash
# --- My daily shortcuts ---
alias ll='ls -lah'
alias ports='ss -tulpn'
alias update='sudo apt update && sudo apt full-upgrade -y'

# Security-flavored shortcuts
alias nse='ls /usr/share/nmap/scripts | grep'
alias myip='curl -s ifconfig.me'
alias grepip='grep -Eo "([0-9]{1,3}\.){3}[0-9]{1,3}"'
alias serve='python3 -m http.server 8000'
alias listen='nc -lvnp'
alias pcap='tshark -r'

Reload:

bash
source ~/.bashrc

Test them:

bash
ll                          # nice long listing
serve                       # serves current dir on :8000
grepip < access.log         # extracts all IPs from a log
listen 4444                 # netcat listener on 4444

When you need arguments, use a function (aliases can't easily take parameters):

bash
scanme() {
  nmap -sV -sC -Pn "$1" -oA "scan_$1"
}
bash
scanme 10.10.10.5

Why it matters: your terminal becomes a custom toolkit. Common tasks drop from 30 keystrokes to 3.


Quick Reference Cheat Sheet

Shortcut / CommandPurpose
Ctrl + RReverse-search command history
sudo !!Re-run previous command with sudo
!$Last argument of previous command
Alt + .Insert last argument of previous command (repeatable)
Ctrl + A / Ctrl + EMove to start / end of line
Ctrl + WDelete previous word
Ctrl + U / Ctrl + YCut / paste text
Ctrl + ZSuspend current process
jobs / fg / bgManage suspended and background jobs
command &Start in background
| sudo tee -a fileAppend to a protected file
{1..10} / {a..z} / {,.bak}Brace expansion / backup shortcut
rm *.{png,jpg,pdf}Delete multiple file types
alias name='cmd'Create a shortcut command
source ~/.bashrcReload shell config

🧪 Your Turn: A 10-Minute Lab

Work through this in your VM. Try to finish it without retyping anything you've already typed.

  1. Create your workspace:

    bash
    mkdir -p ~/PentestLab/{recon,exploit,loot,notes}
  2. Enter it using Alt + .:

    bash
    cd        # then Alt + . to auto-fill the path
  3. Create 10 test files with brace expansion:

    bash
    touch notes/item{1..10}.md && ls notes
  4. Run a privileged command without sudo, then fix it:

    bash
    cat /etc/shadow          # permission denied
    sudo !!                  # now it works
  5. Recall a command with Ctrl + R, pull it to your prompt with →, edit a parameter, run it.

  6. Append a host entry using tee:

    bash
    echo "127.0.0.1 lab.local" | sudo tee -a /etc/hosts
  7. Start a capture, suspend it, resume it:

    bash
    sudo tcpdump -i lo
    # Ctrl + Z → jobs → fg → Ctrl + C
  8. Create a webserver alias and test it:

    bash
    echo "alias webserver='python3 -m http.server 8000'" >> ~/.bashrc
    source ~/.bashrc && webserver

Stretch goal: write a pentest function that scaffolds the folder tree, cds into it, and starts a background tcpdump — all from one command.


The Bigger Picture

None of these tricks is a "hack." They're muscle memory.

The ethical hacker who finishes an assessment early is rarely the one who knows the most tools. It's the one who doesn't waste ten seconds per command.

Pick five of these this week. Add them to your .bashrc. Then come back for the rest.

Where to go next:

  • Explore bind -x to bind custom key sequences in Bash

  • Learn tmux — it turns these process-control tricks into a full workflow

  • Read man bash sections on History Expansion and Brace Expansion

  • Try fzf for fuzzy history search (Ctrl + R on steroids)

Test responsibly. Stay curious. Keep your globs safe. 🐧

Popular Posts