tr — Translate / Delete Characters

What is tr?

tr reads stdin and replaces or deletes characters — not lines or fields. One character maps to another.

OSCP use: Join lines with commas, strip \r from Windows files, remove newlines, uppercase/lowercase transforms.


Syntax

tr SET1 SET2          # translate SET1 → SET2
tr -d SET1            # delete characters in SET1
tr -s SET1            # squeeze repeats
command | tr ...

📌 Common Flags

FlagDescription
-dDelete characters
-sSqueeze repeated characters into one
-cComplement SET1 (use with -d)

📌 Examples

# Uppercase to lowercase
echo "HELLO" | tr 'A-Z' 'a-z'
 
# Delete newlines → join lines (e.g., comma-separated ports)
cut -d'/' -f1 ports.txt | tr '\n' ','
# 80,443,8080,
 
# Delete carriage return (Windows files)
tr -d '\r' < file.txt > clean.txt
cat file.txt | tr -d '\r' > clean.txt
 
# Delete all digits
echo "abc123" | tr -d '0-9'
 
# Squeeze multiple spaces
echo "hello    world" | tr -s ' '

📌 OSCP — Nmap Port List Pipeline

grep "^[0-9]" allports.txt | cut -d'/' -f1 | tr '\n' ',' | sed 's/,$//'
# Result: 22,80,443,8080  (ready for nmap -p)

📌 Quick Cheat Sheet

tr 'a-z' 'A-Z'                    # lowercase → uppercase
tr -d '\r' < file.txt             # strip Windows CR
tr '\n' ',' < file.txt            # newlines → commas
tr -d '0-9' < file.txt            # remove digits
tr -s ' ' < file.txt              # squeeze spaces