Go program stores directory paths
Programming Snapshot – cdbm

© Lead Image © lightwise, 123rf, 123RF.com
When you change directories at the command line, you often find yourself jumping back and forth between known paths. With a utility written in Go, Mike Schilli records the jumps and shows the way back.
While younger coworkers tend to edit their programs with clever IDEs, I still find it most natural to jump to local Git repositories with a quick cd
at the command line and fire up Vim at files with the source code residing there. Typing in the directory path each time is a pain in the ass, and there are usually only half a dozen paths back and forth – so the command line should be able to remember that.
The C shell invented the pushd
and popd
commands many years ago, but wouldn't it be much more convenient to automatically record the directories you visit, store them in a database, and even offer search queries for previously visited directories based on criteria such as frequency or the timestamp of the last visit?
In this issue, a Go program by the name of cdbm
collects the paths accessed by the user during a shell session; the command line user just uses cd,
and some magic glue in the shell's configuration then taps into the $PS1
prompt generator. If the directory changes, cdbm
gets called and stores the new path in an SQLite database on the disk, which later allows search queries whose results can be accessed directly by the user for navigation help. Bash users can modify their .bashrc
file to enable this. On typing a newly introduced command c
, users will see a selection list with the last directories visited (Figure 1). After selecting one of them with the cursor keys and pressing Enter, the shell directly jumps there (Figure 2).
[...]
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

News
-
openSUSE Tumbleweed Ditches AppArmor for SELinux
If you're an openSUSE Tumbleweed user, you can expect a major change to the distribution.
-
Plasma 6.3 Now Available
Plasma desktop v6.3 has a couple of pretty nifty tricks up its sleeve.
-
LibreOffice 25.2 Has Arrived
If you've been hoping for a release that offers more UI customizations, you're in for a treat.
-
TuxCare Has a Big AlmaLinux 9 Announcement in Store
TuxCare announced it has successfully completed a Security Technical Implementation Guide for AlmaLinux OS 9.
-
First Release Candidate for Linux Kernel 6.14 Now Available
Linus Torvalds has officially released the first release candidate for kernel 6.14 and it includes over 500,000 lines of modified code, making for a small release.
-
System76 Refreshes Meerkat Mini PC
If you're looking for a small form factor PC powered by Linux, System76 has exactly what you need in the Meerkat mini PC.
-
Gnome 48 Alpha Ready for Testing
The latest Gnome desktop alpha is now available with plenty of new features and improvements.
-
Wine 10 Includes Plenty to Excite Users
With its latest release, Wine has the usual crop of bug fixes and improvements, along with some exciting new features.
-
Linux Kernel 6.13 Offers Improvements for AMD/Apple Users
The latest Linux kernel is now available, and it includes plenty of improvements, especially for those who use AMD or Apple-based systems.
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.