โ† All cheat sheets

UsefulLinuxTools

Authorized use only. Offensive reference for systems you own or are explicitly permitted to test. You are responsible for staying within the law.

Lesser-known but powerful command-line tools.

================================================================================

TEXT PROCESSING#


    

COLUMN - FORMAT OUTPUT#

# Columnate input
cat data | column -t              # Auto-format columns
cat data | column -t -s ':'       # Custom separator
mount | column -t                 # Pretty mount output

TR - TRANSLATE/DELETE CHARACTERS#

echo "hello" | tr 'a-z' 'A-Z'     # Uppercase
echo "hello" | tr -d 'l'          # Delete characters
echo "hello" | tr -s 'l'          # Squeeze repeated chars
cat file | tr '\n' ' '            # Replace newlines with spaces
cat file | tr -cd '[:print:]'     # Remove non-printable

SORT - SORT LINES#

sort file                         # Alphabetical sort
sort -n file                      # Numeric sort
sort -r file                      # Reverse sort
sort -k2 file                     # Sort by field 2
sort -t',' -k2 file               # Custom delimiter
sort -u file                      # Unique only
sort -h file                      # Human numbers (1K, 2M)

UNIQ - UNIQUE LINES#

sort file | uniq                  # Remove duplicates
sort file | uniq -c               # Count occurrences
sort file | uniq -d               # Show only duplicates
sort file | uniq -u               # Show only unique

WC - WORD COUNT#

wc file                           # Lines, words, chars
wc -l file                        # Lines only
wc -w file                        # Words only
wc -c file                        # Bytes only
wc -m file                        # Characters only

TEE - SPLIT OUTPUT#

command | tee file                # Output to file AND stdout
command | tee -a file             # Append to file
command | tee file1 file2         # Multiple files

HEAD/TAIL - FILE PORTIONS#

head -n 10 file                   # First 10 lines
head -c 100 file                  # First 100 bytes
tail -n 10 file                   # Last 10 lines
tail -f file                      # Follow (live updates)
tail -F file                      # Follow with retry
tail -n +5 file                   # Starting from line 5

================================================================================

FILE OPERATIONS#


    

STAT - FILE INFORMATION#

stat file                         # Full file info
stat -c %s file                   # Size in bytes
stat -c %U file                   # Owner name
stat -c %a file                   # Permissions (octal)
stat -c %y file                   # Modification time

FILE - FILE TYPE#

file document.pdf                 # Identify file type
file *                            # All files in directory
file -i file                      # MIME type
file -b file                      # Brief (no filename)

BASENAME/DIRNAME - PATH PARSING#

basename /path/to/file.txt        # file.txt
basename /path/to/file.txt .txt   # file (remove suffix)
dirname /path/to/file.txt         # /path/to
realpath ./relative/path          # Absolute path
readlink -f symlink               # Follow symlinks

MKTEMP - TEMPORARY FILES#

mktemp                            # Create temp file
mktemp -d                         # Create temp directory
mktemp /tmp/myfile.XXXXXX         # Custom template

================================================================================

SYSTEM MONITORING#


    

HTOP - INTERACTIVE PROCESS VIEWER#

htop                              # Launch (better than top)
# In htop:
# F5: Tree view
# F6: Sort by column
# F9: Kill process
# F4: Filter
# /: Search

IOTOP - DISK I/O MONITOR#

iotop                             # Disk I/O by process
iotop -o                          # Only active processes
iotop -a                          # Accumulated I/O

NETHOGS - NETWORK BY PROCESS#

nethogs                           # Network usage by process
nethogs eth0                      # Specific interface

NCDU - DISK USAGE ANALYZER#

ncdu                              # Interactive disk usage
ncdu /path                        # Analyze specific path
ncdu -x /                         # Exclude other filesystems

DU/DF - DISK USAGE#

du -sh directory                  # Directory size
du -sh * | sort -h                # Size sorted
df -h                             # Filesystem usage
df -i                             # Inode usage

FREE - MEMORY USAGE#

free -h                           # Human readable
free -s 1                         # Update every second

UPTIME - SYSTEM UPTIME#

uptime                            # Uptime and load average

================================================================================

NETWORK TOOLS#


    

SS - SOCKET STATISTICS#

ss -tulpn                         # Listening ports
ss -t                             # TCP connections
ss -u                             # UDP connections
ss -p                             # Show process
ss state established              # Established only

IP - NETWORK CONFIGURATION#

ip addr                           # Show addresses
ip route                          # Show routes
ip link                           # Show interfaces
ip neigh                          # ARP table

HOST/DIG - DNS LOOKUP#

host example.com                  # DNS lookup
dig example.com                   # Detailed DNS
dig +short example.com            # Just the answer

WHOIS - DOMAIN INFO#

whois example.com                 # Domain registration info

NC (NETCAT) - NETWORK UTILITY#

nc -l -p 8080                     # Listen on port
nc host 8080                      # Connect to port
nc -zv host 1-100                 # Port scan

================================================================================

DATA CONVERSION#


    

XXD - HEX DUMP#

xxd file                          # Hex dump
xxd -r hexdump > file             # Reverse (hex to binary)
xxd -p file                       # Plain hex
xxd -i file                       # C include format

HEXDUMP - HEX VIEWER#

hexdump -C file                   # Canonical hex+ASCII

OD - OCTAL DUMP#

od -c file                        # Character output
od -x file                        # Hex output

BASE64 - ENCODE/DECODE#

base64 file                       # Encode
base64 -d encoded                 # Decode
echo "text" | base64              # Encode string

================================================================================

PROCESS MANAGEMENT#


    
pgrep nginx                       # Find PID by name
pgrep -l nginx                    # With process name
pkill nginx                       # Kill by name
pkill -9 nginx                    # Force kill

KILLALL - KILL BY NAME#

killall nginx                     # Kill all nginx
killall -9 nginx                  # Force kill

NOHUP - IGNORE HANGUP#

nohup command &                   # Run immune to hangup
nohup command > out.log 2>&1 &    # With logging

TIMEOUT - LIMIT RUNTIME#

timeout 10s command               # Kill after 10 seconds
timeout 1m command                # Kill after 1 minute
timeout --signal=KILL 10s cmd     # Use SIGKILL

WATCH - REPEAT COMMAND#

watch df -h                       # Update every 2s
watch -n 1 'date'                 # Update every 1s
watch -d ls -l                    # Highlight changes

================================================================================

COMPRESSION#


    

GZIP/GUNZIP#

gzip file                         # Compress (replaces file)
gunzip file.gz                    # Decompress
gzip -k file                      # Keep original
gzip -d file.gz                   # Decompress (same as gunzip)
zcat file.gz                      # View compressed

BZIP2/BUNZIP2#

bzip2 file                        # Compress
bunzip2 file.bz2                  # Decompress
bzcat file.bz2                    # View compressed

XZ/UNXZ#

xz file                           # Compress
unxz file.xz                      # Decompress
xzcat file.xz                     # View compressed

ZIP/UNZIP#

zip archive.zip files             # Create zip
unzip archive.zip                 # Extract
unzip -l archive.zip              # List contents

================================================================================

MISCELLANEOUS#


    

YES - REPEAT STRING#

yes                               # Repeat 'y' forever
yes | command                     # Auto-answer yes
yes "text" | head -n 10           # Repeat 10 times

PARALLEL - PARALLEL EXECUTION#

parallel command ::: a b c        # Run command with each arg
cat list | parallel process {}    # Process in parallel
parallel -j 4 cmd ::: a b c d     # 4 jobs at once

PV - PIPE VIEWER#

pv file | gzip > file.gz          # Progress bar
cat file | pv | process           # Monitor throughput
pv -L 1m file > dest              # Rate limit (1 MB/s)

STRINGS - EXTRACT STRINGS#

strings binary                    # Printable strings
strings -n 10 binary              # Min length 10

EXPECT - AUTOMATE INTERACTIVE#

# Script interactive programs
expect -c 'spawn ssh user@host; expect "password:"; send "pass\n"; interact'

SCREEN - TERMINAL MULTIPLEXER#

screen                            # Start session
screen -S name                    # Named session
screen -r                         # Reattach
screen -ls                        # List sessions
# Ctrl+A D: Detach
# Ctrl+A C: New window
# Ctrl+A N: Next window

DATE - DATE/TIME#

date                              # Current date/time
date +%Y-%m-%d                    # Format: 2024-01-15
date +%s                          # Unix timestamp
date -d @1234567890               # From timestamp
date -d "yesterday"               # Relative dates

CAL - CALENDAR#

cal                               # Current month
cal 2024                          # Full year
cal -3                            # 3 months

================================================================================

QUICK INSTALL (Debian/Ubuntu)#

apt install htop ncdu pv jq tmux parallel nethogs iotop