Go program stores directory paths

Programming Snapshot – cdbm

© Lead Image © lightwise, 123rf, 123RF.com

© Lead Image © lightwise, 123rf, 123RF.com

Article from Issue 228/2019
Author(s):

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).

[...]

Use Express-Checkout link below to read the full article (PDF).

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Programming Snapshot – Go

    To find files quickly in the deeply nested subdirectories of his home directory, Mike whips up a Go program to index file metadata in an SQLite database.

  • Perl: Archiving PDFs

    This month you’ll learn how to place articles in a private PDF archive and how to use a database to access those articles at a later time.

  • Perl: Slurping Kindle Highlights

    On the Amazon Kindle eBook reader, you can save personal clippings, or “highlights,” in a file; later, you can connect the Kindle to a USB port on a Linux machine and grab the data with a Perl script that stores it in a database.

  • bd, autojump, and Fasd

    Bd, autojump, and Fasd improve the workflow for command-line aficionados thanks to quick navigation in the filesystem.

  • Perl: amtrack

    If you are a bargain hunter, you might enjoy this Perl script that monitors price developments at Amazon and alerts you if Amazon suddenly drops the prices on the products you have been watching.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News