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!
저자 소개
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.
채널별 검색
오토메이션
기술, 팀, 인프라를 위한 IT 자동화 최신 동향
인공지능
고객이 어디서나 AI 워크로드를 실행할 수 있도록 지원하는 플랫폼 업데이트
오픈 하이브리드 클라우드
하이브리드 클라우드로 더욱 유연한 미래를 구축하는 방법을 알아보세요
보안
환경과 기술 전반에 걸쳐 리스크를 감소하는 방법에 대한 최신 정보
엣지 컴퓨팅
엣지에서의 운영을 단순화하는 플랫폼 업데이트
인프라
세계적으로 인정받은 기업용 Linux 플랫폼에 대한 최신 정보
애플리케이션
복잡한 애플리케이션에 대한 솔루션 더 보기
오리지널 쇼
엔터프라이즈 기술 분야의 제작자와 리더가 전하는 흥미로운 스토리