bat Linux command – A cat clone with written in Rust

The cat (short for concatenate) command is one of the most frequently used flexible commands on Linux and Unix-like operating systems. Say hello to bat Linux command, which is a cat command written in Rust programming language. The bat command comes with syntax highlighting, git integration, and works as is a drop-in cat command replacement. Let us see how to install bat on Linux and Unix system for fun and profit.

ADVERTISEMENTS

Say hello to bat Linux command

Let us see some cool features of bat command:

  1. Syntax highlighting – Bat supports syntax highlighting for a large number of programming and markup languages.
  2. Git integration – Bat communicates with git to show modifications in left side.
  3. Automatic paging – The command can pipe its own output to less if the output is too large for one screen.
  4. Display and highlight non-printable characters.
  5. A user-friendly command-line interface.
  6. And of course, for file concatenation, including all of the cat command functionality.

Installation

We need to use the system package manager. Let us see some famous Linux distro examples for installing bat command. First open the Terminal application and then type commands as per your operating systems.

Installing bat on Ubuntu and Debian Linux

You need Ubuntu 19.10 or above. On Debian bat is currently only available on the unstable “Sid” branch and will be part of Debian 11. See how to install it on Debian 10 here.

Simply run the following apt command/apt-get command:
$ sudo apt install bat

[sudo] password for vivek: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libgit2-28 libhttp-parser2.9 libmbedcrypto3 libmbedtls12 libmbedx509-0
The following NEW packages will be installed:
  bat libgit2-28 libhttp-parser2.9 libmbedcrypto3 libmbedtls12 libmbedx509-0
0 upgraded, 6 newly installed, 0 to remove and 4 not upgraded.
Need to get 2,274 kB of archives.
After this operation, 6,279 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://archive.ubuntu.com/ubuntu focal/universe amd64 libhttp-parser2.9 amd64 2.9.2-2 [21.8 kB]
Get:2 http://archive.ubuntu.com/ubuntu focal/universe amd64 libmbedcrypto3 amd64 2.16.4-1ubuntu2 [150 kB]
...
Fetched 2,274 kB in 4s (583 kB/s)
Selecting previously unselected package libhttp-parser2.9:amd64.
(Reading database ... 285273 files and directories currently installed.)
Preparing to unpack .../0-libhttp-parser2.9_2.9.2-2_amd64.deb ...
Unpacking libhttp-parser2.9:amd64 (2.9.2-2) ...
Selecting previously unselected package libmbedcrypto3:amd64.
Preparing to unpack .../1-libmbedcrypto3_2.16.4-1ubuntu2_amd64.deb ...
Unpacking libmbedcrypto3:amd64 (2.16.4-1ubuntu2) ...
...
Selecting previously unselected package bat.
Preparing to unpack .../5-bat_0.12.1-1build1_amd64.deb ...
Unpacking bat (0.12.1-1build1) ...
Setting up libmbedcrypto3:amd64 (2.16.4-1ubuntu2) ...
....
Setting up bat (0.12.1-1build1) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for libc-bin (2.31-0ubuntu9) ...

Alpine Linux

Execute the apk command $ sudo apk add bat

Arch Linux install bat

Run the pacman command:
$ sudo pacman -S bat

Fedora Linux user try the following dnf command

sudo dnf install bat

On Gentoo Linux

We can use the emerge command:
$ sudo emerge sys-apps/bat

For Void Linux

Try the xbps-install:
$ sudo xbps-install -S bat

FreeBSD install bat with the help of pkg command

$ sudo pkg install bat
Of course. FreeBSD users can build it from the FreeBSD ports too:
# cd /usr/ports/textproc/bat
# make install

openSUSE user try the zypper command

$ sudo zypper install bat

Apple macOS Unix users try any one of the following method

You can install bat with Homebrew:
brew install bat
Or install bat with MacPorts:
port install bat

MS Windows users

Open Windows prompt and type:
choco install bat
## OR ##
scoop install bat

Using bat on Linux and Unix

Like cat command, bat often works out-of-the-box on Linux or Unix. No extra configuration needed. So all you have to do is type:
batcat filename
bat filename

Please note that on some distros it is called batcat to avoid confusion with other tool named bat. For example let us try to display a file named ~/bin/backupme
$ batcat ~/bin/backupme
$ bat ~/bin/backupme


I am going to define a bash shell alias using the alias command:
$ alias cat="batcat"
$ cat /etc/passwd

See “How to create a permanent Bash alias on Linux/Unix” for more info.

Turn off decorations

Pass the -p option to the batcat:
$ cat -p ~/bin/backupme

Only print the specified range of lines for each file

In the following example show lines 35 to 42 only:
$ cat -r 35:42 /etc/hosts
Pints lines 1 to 20:
$ cat -r :20 /etc/hosts
Want to show from lines 20 to the EOF:
$ cat -r 20: /etc/hosts

Themes

Get a list of supported themes for syntax highlighting:
cat --list-themes
Set the theme called ansi-dark for syntax highlighting
cat --theme ansi-dark /path/to/file

Getting help

Use the man command or pass the --help option to the batcat:
$ cat --help
$ batcat --help
$ man batcat

Conclusion

In this quick post, we learned about installing and using bat command on Linux and Unix-like systems using the CLI. Bat is a perfect modern tool for desktop users, but I won’t install it on my servers. Give it a try and let us know what you think about the cool tool written in Rust programming language.

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source & DevOps topics via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
1 comment… add one
  • Zigzac Jul 17, 2020 @ 21:14

    I love this bat utility. Why not replace cat with bat permanently?

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.