Subscribe to the feed

Sysadmins love their one-liners. In fact, one of Enable Sysadmin's most popular articles of 2021 was 20 one-line Linux commands to add to your toolbox. I was trying to remember one of my more esoteric command strings a few years ago when I realized that keeping all of those one-liners straight can be a cognitive burden. Do you try to remember them? Maybe you simply bookmark your favorite ones or maintain a useful_stuff.txt file on your desktop, as I did for years?

To solve this problem, I wrote an open source tool called Rucksack. Rucksack is a place to store your useful one-liners. Place your favorite commands into a nested YAML dictionary, and the Rucksack interface provides you with friendly autocompletion for both commands and arguments.

I'll introduce you to Rucksack by implementing a few of my favorite commands from the Enable Sysadmin article.

Get started

First, you need to install Rucksack. Rucksack is available as a Python package for Python versions greater than 3.8 and can be installed via pip (the exact command may vary depending on your system):

$ python3 -m pip install --user rucksack

The installation adds the ruck command to your PATH.

Rucksack also requires a configuration file. Rucksack config files can live in a few places, but you will most commonly use either your current working directory or ~/.config/rucksack.

This article shows you how to get started with a rucksack.yaml file in your working directory. You'll need jq for one of the examples, which is not generally installed by default. You can find jq installation information on the tool's website.

A simple example

To appreciate how Rucksack works, I'll start with a simple example, the lsblk command (included in the one-liners article). The command displays the name and size of block devices. Add the following to your rucksack.yaml file in your current working directory:

enable-sysadmin:
  get-bdinfo:
    command: lsblk --json | jq -c '.blockdevices[]|[.name,.size]'

This configuration allows Rucksack to create an autocompletion at enable-sysadmin get-bdinfo. You can try it out against your local system by running ruck --host localhost. Rucksack shells out commands locally when you specify localhost or 127.0.0.1. If you identify a remote host, Rucksack connects to it over SSH.

[ Keep the Bash shell scripting cheat sheet close at hand to make writing scripts easier. ]

Add arguments

The previous example demonstrates a simple, static command. However, your one-liners likely require arguments to operate. Rucksack accepts user arguments and renders them out with Jinja2. I will demonstrate this with a very useful command to locate recent errors with journalctl.

Update your rucksack.yaml so that it looks like this:

enable-sysadmin:
  get-bdinfo:
    command: lsblk --json | jq -c '.blockdevices[]|[.name,.size]'
  get-errors:
    command: journalctl --no-pager --since {{ date }} --grep 'fail|error|fatal' --output json|jq '._EXE' | sort | uniq -c | sort --numeric --reverse --key 1
    args:
      - date:
          mandatory: True
          default: today

Rucksack now accepts a date argument with the journalctl command, which defaults to "today" if you do not specify anything else.

Create dynamic arguments

Arguments are very useful, and Rucksack also allows you to specify a list of potential arguments for a command. However, you often don't know the potential argument values in advance. For example, consider this one-liner to find the size of a package:

$ rpm --queryformat='%12{SIZE} %{NAME}\n' -q java-11-openjdk-headless

You might want to extend this to find the size of a package installed on your system. First, you need to have a list of packages that are installed locally. Rucksack supports providing dynamic arguments to a user based on the output of another command.

That might sound confusing, so update your rucksack.yaml file and give it a try:

enable-sysadmin:
  get-bdinfo:
    command: lsblk --json | jq -c '.blockdevices[]|[.name,.size]'
  get-errors:
    command: journalctl --no-pager --since {{ date }} --grep 'fail|error|fatal' --output json|jq '._EXE' | sort | uniq -c | sort --numeric --reverse --key 1
    args:
      - date:
          mandatory: True
          default: today
  get-installed-size:
    command: rpm --queryformat='%12{SIZE} %{NAME}\n' -q {{ package_name }}
    args:
      - package_name:
          mandatory: True
          from_command: rpm -qa

Rucksack now provides potential values for the package_name argument by running rpm -qa and presenting the results as an autocompletion. This is my favorite Rucksack feature, and it allows you to write some really useful one-liners.

Get one-liners out of your head

Like many admins, I've spent years accumulating a collection of quick commands in my head. Unfortunately, committing them to memory can be difficult, and storing them in a text file is clumsy. Rucksack is my initial attempt to solve this problem. It has few dependencies, a friendly YAML-based configuration syntax, and an easy installation process.

If you're interested in learning more, I encourage you to check out the project on GitHub and run through the tutorial. Please feel free to report any issues or feature requests, and best of luck on your shell journey!


About the author

Anthony Critelli is a Linux systems engineer with interests in automation, containerization, tracing, and performance. He started his professional career as a network engineer and eventually made the switch to the Linux systems side of IT. He holds a B.S. and an M.S. from the Rochester Institute of Technology.

Read full bio
UI_Icon-Red_Hat-Close-A-Black-RGB

Browse by channel

automation icon

Automation

The latest on IT automation for tech, teams, and environments

AI icon

Artificial intelligence

Updates on the platforms that free customers to run AI workloads anywhere

open hybrid cloud icon

Open hybrid cloud

Explore how we build a more flexible future with hybrid cloud

security icon

Security

The latest on how we reduce risks across environments and technologies

edge icon

Edge computing

Updates on the platforms that simplify operations at the edge

Infrastructure icon

Infrastructure

The latest on the world’s leading enterprise Linux platform

application development icon

Applications

Inside our solutions to the toughest application challenges

Original series icon

Original shows

Entertaining stories from the makers and leaders in enterprise tech