Practical Linux Tricks for the Aspiring Hacker
A curated list of fanciful Linux tricks I use to bolster my command-line prowess and activate Sage Mode.
This is a collection of commands I've picked up over the last few years, which I've found immensely useful. My favourite ones are probably:
less
: search/filter on a file or long text^r
: reverse search!$
: last argument of previous command
By "favourite", I mean I've used these commands a lot, and they've drastically increased my productivity.
Cool Stuff
Control (^
) Commands
Reverse/Forward Search: for those long commands stashed in history. Works in PowerShell and REPLs too!
Ternary Expression
Clear screen. Useful for graphical hiccups.
Run shell script without chmod +x
.
Tree view of files.
Strings
Double-Quotes vs. Single-Quotes
- Double-quotes allow variable expansion and command substitution.
- Single-quotes don't. Prefer single-quotes for simple strings.
Multi-Line / Escape
Prefix the string with $
.
Escape Single-Quotes
Example
Multi-Line Strings.
Find words containing 't
in comma-separated line.
Previous-Command Tricks
$?
: exit code of previous command- By convention, 0 means no error. Non-0 implies an error occurred.
!!
: previous command!$
or$_
: last argument of previous command
Examples
Retry with sudo.
Found an interesting directory, but forgot to cd.
Rename file in folder from file.txt to booyah.md.
Other Useful Commands (stolen from here)
!!:n
- nth argument from previous command!^
- first argument (after the program/built-in/script) from previous command!*
- all arguments from previous command!n
- command numbern
fromhistory
!pattern
- most recent command matchingpattern
!!:s/find/replace
- last command, substitutefind
withreplace
Redirection
Powerful Utilities
awk
: filter lines, filter columns, math, scripting, etc.sed
: filter/replace textgrep
: filter linescut
: filter columnstr
: replace/remove characterswc
: count characters/bytes/wordsfind
: find files in folder, execute command for each file with-exec
xargs
: feed arguments into commands, simple cmdline multi-processing
I won't cover too much of these commands here, as tons of articles already cover them. And you can browse examples online or in their man
pages.
awkward things
awk - Cut
awk - Filtering
Without entering the scripting environment {...}
, awk
will run filters against each line.
awk - Math
awk - Scripting
Script variables. (Useful for configuring row/column delimiters.)
- RS: Record Separator (rows)
- FS: Field Separator (columns)
- ORS: Output Row Separator
- OFS: Output Field Separator
- NR: Record Number (current row, 1-indexed) [read-only]
- NF: Number of Fields [read-only]
grep
Useful Flags
grep – Find String in Files
-r
or-R
is recursive,-n
is line number, and-w
stands for match the whole word.-l
can be added to just give the file name of matching files.-e
is the pattern used during the search
Ref: https://stackoverflow.com/a/16957078/10239789
xargs
xargs is a versatile command-line utility that allows efficient execution of commands, making it a powerful tool for automation and batch processing.
Interesting options:
Examples
Combine multiple lines into 1 line.
Multi-Processing: Execute ./do-something-to-file.sh <file>
on multiple files, with at most 4 processes.
Multi-Processing: Port Scan with Ports 1-1000 through proxychains
.
Other Utilities
Directory Stack
pushd
/popd
Example
less
less
is a powerful text viewer (read-only), with capabilities to navigate, search, and filter lines in a file or long text.
Get some help. See all commands:
less - Nice Options
You can turn on/off these options inside less
by typing -I<Enter>
, -R<Enter>
, or -N<Enter>
. This is useful if you forget to turn them on beforehand (e.g. after curling a web request).
less - Navigation
less - Search / Filtering
less - Scrolling
Personally, I prefer less+F
over tail -f
.
Use ^c
to exit the feed.
less - Working with Multiple Files
less
also works with multiple files passed in the command line, e.g. less *.txt
.
More commands in man less
.
Processes
fg/bg - "I'll be back."
Shells allow you to move processes between the foreground (which accepts interactive input) and background (to run things which don't require input).
Example
Start an HTTP server on port 8080.
The process is started in the background with job number 1, PID 17999.
To kill the process:
or...
Process ID (PID) and Job Number are two different things.
- PIDs apply to all users in the entire system, and are assigned by the kernel.
- Job Numbers apply to the current shell, and are numbered linearly from 1 onwards.
View Running Procs
Combine with grep
/less
for filtered results.
Networking
IP and Ports
IP Addresses and Networks
Get Our Public IP
Open Ports/Sockets
-a
: all sockets-n
: numeric addresses-p
: associated processes
Listen/Connect
Download Files
Upload Files
- By default,
uploadserver
starts a server at port 8000. - Get our IP from
ifconfig
.
git gud
Git commands for completeness.
git tree
Fun watch: So You Think You Know Git?
vim
Haha. Nope.
Not covering that here.
How to Exit Vim
Obligatory.
Okay, that's enough vim.
Useful Things
Set line numbers.
Hacky Hack Hack
Generate Bytes
Buffer overflow for fun and profit.
echo
perl (good for repetitive sequences)
I've mentioned this elsewhere, but I'll repeat it here: I don't recommend using Python 3 to generate strings on-the-fly, as its string/byte-string mechanics are unintuitive. Prefer perl
or echo
instead.
For example: python -c 'print("\xc0")'
prints \xc3\x80
(À) instead of \xc0
. Why? Because the Python string "\xc0"
is interpreted as U+00C0, which is \xc3\x80
in UTF-8.
Printing bytes in Python is difficult to do concisely.
Simple Binary Analysis
Look for strings.
Look for strings and print addresses (in hex)!
Tracing
strace
- trace system calls (open, read, write, etc.)ltrace
- trace library (glibc) calls
Comments are back! Privacy-focused, without ads, bloatware, and trackers. Be one of the first to contribute to the discussion — I'd love to hear your thoughts.