bat — syntax-highlighting cat
💬 "bat command"
bat is a syntax-highlighting file viewer that replaces cat. It is built with Rust and supports color, line numbers, and git change display by default.
With an almost identical command structure to cat, it can be easily integrated into existing scripts.
- Concept: Prints file contents with syntax highlighting.
- cat replacement: Displays files from the same location as cat but provides more information.
- --number: Displays line numbers.
- --diff: Shows git changed files in diff mode.
Installation and Download
bat is installed via a package manager. On Debian/Ubuntu, the package name is batcat.
| Environment | Command |
|---|---|
| Ubuntu / Debian | sudo apt install batcat |
| Fedora / RHEL | sudo dnf install bat |
| Arch | sudo pacman -S bat |
| macOS (brew) | brew install bat |
| Rust cargo | cargo install bat |
| Binary download | github.com/sharkdp/bat/releases |
To verify the installation, use bat --version or batcat --version.
Commonly Used Options
| Option | Description |
|---|---|
| -n / --number | Displays line numbers |
| --style=numbers,changes | Enables line numbers and git change display |
| --diff | Outputs git changed files in diff mode |
| -A / --show-all | Displays invisible characters like tabs and line breaks |
| --theme=Dracula | Changes the theme |
Three Essential Command Combinations
bat -A config.conf— Check configuration file including invisible charactersbat --diff src/main.py— Check git changes with syntax highlightingbat --line-range 1:50 error.log— Check a specific section of a log file
💬 "Show me an example of checking configuration files on a real server"
Here is a step-by-step breakdown of common situations encountered on servers in production.
Step 1 · Verify Installation
First, check if it's installed, and if not, install it.
bat --version || batcat --version || sudo apt install -y batcat
Step 2 · Check Configuration File
View the nginx configuration file with syntax highlighting.
bat -n /etc/nginx/nginx.conf
Step 3 · Specific Section Only
Extract just the error section from the log file.
bat --line-range 100:150 /var/log/nginx/error.log
Actual Execution Screen
This is the result of checking the nginx configuration file with bat after installation. Colors are divided according to syntax, and line numbers are displayed.
Server in production
In Practice, It's Used Like This
1. Configuration File Review
Situation — Find typos in nginx or app configuration files.
bat -A /etc/nginx/sites-available/default
Result — Arrows instead of tabs, $ symbols instead of line breaks are shown, allowing you to check hidden characters.
Key Point — The -A option allows you to visually distinguish between empty lines and spaces.
2. Script Review
Situation — Review the logic of a deployment script with syntax highlighting.
bat --style=numbers deploy.sh
Result — With line numbers attached, it's easy to tell where changes were made.
Key Point — The --style=numbers option is useful during code reviews.
Actual Execution Screen
This is the result of checking git changed files in diff mode. Added and deleted lines are distinguished by color.
!bat command execution result 2
Server in production
Points to Note
- On Debian/Ubuntu, the command name may be batcat.
- bat uses a lot of memory when reading large files. For logs of several GB, use it with head.
- If the terminal doesn't support 256 colors, colors may appear awkward.
- If bat is set as the default viewer instead of less, scripts may break.
Source (HTML): https://games.aidebatehub.com/programs/linux/modern/bat.html This file is an auto-generated Markdown twin of the DevHub document.
AI Knowledge Hub
Visitor Comments