In this post I wanted to go over a few CLI utilities written in Rust that I am excited about. There are a lot of different Rust CLI tools out there, but I wanted to focus on ones that replace some of the GNU Core Utilities with Rust alternatives. The tools I will be talking about are exa
, ripgrep
, and bat
.
You may be wondering why would you need alternatives for trusted utilities that have been around for decades? Well, a few reasons. First, they are written in Rust! Which for me is reason alone to use them. Secondly, some of these tools are makeovers with pretty colors and sane defaults, and sometimes they are even faster than the original. Some of the effort behind these tools goes to support the RedoxOS project which I highly recommend checking out if you haven't.
Side note: If you want to follow along I'm assuming you are using the Debian operating system. You can also install these tools with cargo directly.
Side note 2: Unfortunately, I wasn't able to show the proper colors for these tools, because of the limitations of my blogging skills. Just keep in mind that each of these tools has great color schemes that significantly help readability.
exa
is a cli tool for listing files in a directory. It serves to be an alternative to the ls
command. The improvements I would consider are nice colors, sane defaults, and extra goodies.
It can be installed like so:
sudo apt install exa
Besides using ls
to list files you can also see how big a file is. At my job I work with a lot of large csv and json files, so this comes in handy. The only downside to this is that to list the filesizes as not bytes you have to do this incantation:
fancycade[test_dir]: ls -l --block-size=M
total 1M
-rw-r--r-- 1 shiba shiba 1M Sep 1 14:25 data.csv
-rw-r--r-- 1 fancycade fancycade 0M Sep 1 14:03 README
drwxr-xr-x 2 fancycade fancycade 1M Sep 1 14:03 src
Which only gives the file size in Megabytes. Since it rounds down or up it doesn't giving clear information as to the actual size of your file.
Whereas exa
chooses the appropriate unit based on the file size without listing the unit, while also being much easier to remember.
fancycade[test_dir]: exa -l
.rw-r--r-- 786k fancycade 1 Sep 14:25 data.csv
.rw-r--r-- 182 fancycade 1 Sep 14:24 README
drwxr-xr-x - fancycade 1 Sep 14:03 src
Another feature that exa
has that ls
doesn't have is the ability to list the files in a directory as a tree. Normally this would be done with the tree
utility, but now we don't even need to install it if we have exa
. For example:
fancycade[test_dir]: exa --tree
.
├── data.csv
├── README
└── src
└── generate_data.rb
Typical tree representation coming from the same tool. exa
is pretty sweet, highly recommend giving it a try.
ripgrep
is a Rust alternative to the well known tool grep
. Who could've guessed?! Now this tool is one that I'm the least familiar with because I'm not a master grep
user. However, I have found a few use cases that I find to be very helpful. The cool thing about ripgrep
is that it is actually faster than the original. Which is crazy, since grep
was always touted as extremely fast.
To install ripgrep
you simply do this:
sudo apt install ripgrep
Now to use ripgrep
we type the alias rg
. Imagine a scenario where you are looking for a file in a directory that has a lot of stuff in it. You run exa
and all you see is a wall of text. One clever trick is to pipe the output of exa
into ripgrep
to filter only the relevant filenames. So if I know I'm looking for a csv file, I can do something like this:
fancycade[test_dir]: exa | rg csv
data.csv
Instead of printing out all the contents of the current directory it only outputs files that include csv in the filename. This is only the tip of the iceberg of what you can do with this tool, so I highly suggest experimenting and trying things out.
bat
is a Rust alternative to the cat
utility with a few extra goodies. It also combines the features of less
as well, so it can be considered a cross between the two. The problem with cat
is that it can be hard to read large files. Which is why less
is useful. On top of that bat
provides formatting for a lot of common filetypes like markdown or source code. And line numbers! Which is really helpful when looking at tabular data such as csvs.
Unfortunately, as of this post, bat
isn't in Debian stable (buster). However, there is a workaround, and it will probably be available in Ubuntu fairly soon. This workaround is not typically recommended by the Debian community because it does not respect their ideas of a "stable" version. So make sure to do this with caution.
We begin the process by editing our /etc/apt/sources.list
with nano
(make sure you have proper permissions):
sudo nano /etc/apt/sources.list
Add this line to the file:
deb http://deb.debian.org/debian/ sid main
This allows apt to get packages in the Debian unstable (aka sid) archive.
You have to update the package lists:
sudo apt update
Now you can install bat
:
sudo apt install bat
As I mentioned earlier, it is not recommended to use packages from Debian unstable so I highly suggest removing the line we added to the /etc/apt/sources.list
because we don't want to accidentally grab other unstable packages later.
Once we have bat
installed we can get started. We can look at a csv file (list of popular baby names) like this:
fancycade[test_dir]: bat data.csv
───────┬──────────────────────────────────────────────────────
│ File: data.csv
───────┼──────────────────────────────────────────────────────
1 │ Year of Birth,Gender,Ethnicity,First Name,Count,Rank
2 │ 2016,FEMALE,ASIAN AND PACIFIC ISLANDER,Olivia,172,1
3 │ 2016,FEMALE,ASIAN AND PACIFIC ISLANDER,Chloe,112,2
4 │ 2016,FEMALE,ASIAN AND PACIFIC ISLANDER,Sophia,104,3
5 │ 2016,FEMALE,ASIAN AND PACIFIC ISLANDER,Emma,99,4
6 │ 2016,FEMALE,ASIAN AND PACIFIC ISLANDER,Emily,99,4
7 │ 2016,FEMALE,ASIAN AND PACIFIC ISLANDER,Mia,79,5
8 │ 2016,FEMALE,ASIAN AND PACIFIC ISLANDER,Charlotte,59,6
9 │ 2016,FEMALE,ASIAN AND PACIFIC ISLANDER,Sarah,57,7
10 │ 2016,FEMALE,ASIAN AND PACIFIC ISLANDER,Isabella,56,8
11 │ 2016,FEMALE,ASIAN AND PACIFIC ISLANDER,Hannah,56,8
12 │ 2016,FEMALE,ASIAN AND PACIFIC ISLANDER,Grace,54,9
13 │ 2016,FEMALE,ASIAN AND PACIFIC ISLANDER,Angela,54,9
14 │ 2016,FEMALE,ASIAN AND PACIFIC ISLANDER,Ava,53,10
15 │ 2016,FEMALE,ASIAN AND PACIFIC ISLANDER,Joanna,49,11
16 │ 2016,FEMALE,ASIAN AND PACIFIC ISLANDER,Amelia,44,12
17 │ 2016,FEMALE,ASIAN AND PACIFIC ISLANDER,Evelyn,42,13
18 │ 2016,FEMALE,ASIAN AND PACIFIC ISLANDER,Ella,42,13
19 │ 2016,FEMALE,ASIAN AND PACIFIC ISLANDER,Arya,42,13
20 │ 2016,FEMALE,ASIAN AND PACIFIC ISLANDER,Ariana,40,14
21 │ 2016,FEMALE,ASIAN AND PACIFIC ISLANDER,Maya,39,15
22 │ 2016,FEMALE,ASIAN AND PACIFIC ISLANDER,Alina,39,15
23 │ 2016,FEMALE,ASIAN AND PACIFIC ISLANDER,Fiona,35,16
24 │ 2016,FEMALE,ASIAN AND PACIFIC ISLANDER,Ashley,34,17
25 │ 2016,FEMALE,ASIAN AND PACIFIC ISLANDER,Anaya,34,17
26 │ 2016,FEMALE,ASIAN AND PACIFIC ISLANDER,Fatima,34,17
:
Notice the line numbers on the left. Like less
we can navigate the file with up/down arrows and exit by pressing 'q'.
bat
is really great, and has become something I use on a daily basis. It is amazing what subtle differences in tooling can do to your workflow. Hopefully, you found this post interesting, and are excited to try cli tools written in Rust like I am.
This is fancycade, signing off.
Content for this site is CC-BY-SA.