10 Linux Terminal Tricks Every Ethical Hacker Should Know
Stop retyping commands. Start moving faster than the clock.
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:
(reverse-i-search)`nmap': nmap -sV -sC -Pn -p- --min-rate 2000 192.168.1.0/24 -oA lab_scanHere'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 + Ragain → cycle through older matchesCtrl + 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:
$ systemctl restart sshd Failed to restart sshd.service: Interactive authentication required.
Don't retype it. Just run:
sudo !!
The shell expands !! into your entire previous command:
sudo systemctl restart sshdA few history expansions worth memorizing:
!! # 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:
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:
wget https://example.com/wordlists/raft-medium-directories.txtNow you want to use that exact path in your next tool. Type wc -l, press Alt + ., and:
$ wc -l raft-medium-directories.txt
A real recon chain:
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:
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:
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.
| Key | Action |
|---|---|
Ctrl + A | Jump to start of line |
Ctrl + E | Jump to end of line |
Ctrl + W | Delete the previous word |
Ctrl + K | Delete from cursor to end |
Alt + B / Alt + F | Move word by word |
The fix:
Press
↑to recall the commandCtrl + A→ cursor jumps to the startType
sudo→ doneCtrl + E→ back to the end, hit Enter
Another use — Ctrl + W in action:
Before:
nano /etc/apache2/sites-enabled/example.confPress Ctrl + W to delete the filename:
After:
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:
echo "10.10.14.5 target.htb" >> /etc/hosts
bash: /etc/hosts: Permission denied
So you try:
sudo echo "10.10.14.5 target.htb" >> /etc/hosts
It still fails. Here's why:
The shell handles the
>>redirection beforesudoruns. The shell — running as your user — is the one trying to open/etc/hostsfor writing.sudoonly elevatesecho, which does nothing useful.
The correct way:
echo "10.10.14.5 target.htb" | sudo tee -a /etc/hosts
| Form | Effect |
|---|---|
... | sudo tee file | Overwrite file |
... | sudo tee -a file | Append to file |
... | sudo tee file > /dev/null | Write without screen echo |
Real example — hardening SSH in a lab:
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.
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:
mkdir -p pentestlab/{recon,exploit,loot,notes} touch pentestlab/recon/scan{1..3}.nmap touch pentestlab/loot/cred{1..5}.txt tree pentestlab
Output:
pentestlab
├── exploit
├── loot
│ ├── cred1.txt ... cred5.txt
├── notes
└── recon
├── scan1.nmap ... scan3.nmapThe backup trick you'll use forever:
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.
rm *.{png,jpg,pdf}
But always preview first:
# 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}
⚠️
rmhas no undo and no recycle bin. On an engagement, a misplaced glob can destroy your evidence. Preview withlsevery single time.
8. Cut and Paste on the Command Line
You're mid-way through a capture command:
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
# 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:
sudo tcpdump -i eth0
…and realize you need your shell back, but you don't want to kill the capture.
| Key/Command | Action |
|---|---|
Ctrl + Z | Suspend (pause) the running process |
jobs | List suspended/background jobs |
fg | Resume most recent job in foreground |
bg | Resume most recent job in background |
fg %2 / bg %1 | Target a specific job |
disown | Detach so it survives shell exit |
$ 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:
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.
alias ll='ls -lah'
Now ll runs ls -lah. To make it permanent, open ~/.bashrc:
nano ~/.bashrcAdd a block:
# --- 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:
source ~/.bashrcTest them:
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):
scanme() { nmap -sV -sC -Pn "$1" -oA "scan_$1" }
scanme 10.10.10.5Why it matters: your terminal becomes a custom toolkit. Common tasks drop from 30 keystrokes to 3.
Quick Reference Cheat Sheet
| Shortcut / Command | Purpose |
|---|---|
Ctrl + R | Reverse-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 + E | Move to start / end of line |
Ctrl + W | Delete previous word |
Ctrl + U / Ctrl + Y | Cut / paste text |
Ctrl + Z | Suspend current process |
jobs / fg / bg | Manage suspended and background jobs |
command & | Start in background |
| sudo tee -a file | Append 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 ~/.bashrc | Reload 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.
Create your workspace:
mkdir -p ~/PentestLab/{recon,exploit,loot,notes}
Enter it using
Alt + .:cd # then Alt + . to auto-fill the path
Create 10 test files with brace expansion:
touch notes/item{1..10}.md && ls notes
Run a privileged command without sudo, then fix it:
cat /etc/shadow # permission denied sudo !! # now it works
Recall a command with
Ctrl + R, pull it to your prompt with→, edit a parameter, run it.Append a host entry using
tee:echo "127.0.0.1 lab.local" | sudo tee -a /etc/hosts
Start a capture, suspend it, resume it:
sudo tcpdump -i lo # Ctrl + Z → jobs → fg → Ctrl + C
Create a
webserveralias and test it: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 -xto bind custom key sequences in BashLearn
tmux— it turns these process-control tricks into a full workflowRead
man bashsections on History Expansion and Brace ExpansionTry
fzffor fuzzy history search (Ctrl + Ron steroids)
Test responsibly. Stay curious. Keep your globs safe. 🐧
