Linux Terminal Command: tr
The tr command is an essential tool in File Manipulation & Viewing. In this tutorial, we will explore what tr does, look at everyday examples, and cover advanced options to supercharge your command-line workflow.
Concept & Explanation
The tr command translates, squeezes, or deletes characters from standard input, writing to standard output.
Common Options & Syntax
tr [options] [arguments]
Here are the most common flags used with tr:
- Simple Usage: Basic default commands.
- Detailed View: Shows diagnostic information.
- Advanced Actions: Can chain parameters for scripting.
1. Interactive Example (Simple)
Here is how most people run the command:
# Example
echo 'hello' | tr 'a-z' 'A-Z'
What it does: Converts ‘hello’ to uppercase ‘HELLO’.
2. Power-User Example (Advanced)
For scripting and advanced diagnostics, use this configuration:
# Advanced
tr -cd '[:print:]' < raw_input.txt && tr -s ' ' < input.txt
What it does: Deletes all non-printable characters (-c complements target, -d deletes), and squeezes repeated spaces into a single space (-s).
⚙️ Warning & Common Pitfalls
[!WARNING]
tronly reads data from standard input. It cannot take file paths as direct arguments. You must feed files using redirection (<).
🔗 Related Commands
Here are some related posts on cli_tty1 you might want to check out: