Multiplatform Directory Bookmarks on the Command Line
This article discusses how to manage directory bookmarks on the command line for efficient use of the character terminal.
Join the DZone community and get the full member experience.
Join For FreeDo you work with Java projects and switch between different file system directories in the character terminal? Then you might find the application described in this article useful.
How It Works
A command can be written (in the terminal) on the currently visited directory sdf d1
, where the parameter d1
is an alphanumeric text representing the name of the bookmark. From that point on, you can return to the same directory at any time later with the command cdf d1
.
Multiple bookmarks can be assigned to one directory. Used bookmark names can be recycled: saving the same bookmark repeatedly will silently overwrite the original entry. Bookmarks are case-sensitive and are saved in CSV text format (tab-delimited) in the user's home directory named .directory-bookmarks.csv so they can be easily edited with a regular text editor. A typical reason for editing may be to change the directory structure, but the mentioned file may also be useful when transferring the project to another computer. The following environments are supported:
- Windows PowerShell
- Windows Command Prompt
- Bash (Unix shell)
- Git BASH for Windows
The original implementation was created 30 years ago (sometime during 1993) as a Unix C-shell script. After switching to Linux, I modified the script slightly for Bash and have used it in that form until now. This year, I decided to rewrite the tool in Java due to the planned work in Windows. I then implemented the new solution in a single file (more precisely, in a single Java class) with the name DirectoryBookmarks.java
so that it can be run in the Java 17 environment even without previous compilation. The class accepts the original data format, and some functions have been added. It makes sense to start using the above-mentioned shortcuts (application commands) only after their integration into the character terminal environment. Let's review:
sdf d1 [comment]
: The name of the command was inspired by the words “Save Directory with a bookmark to the File." The positionparameterd1
is required and represents the bookmark name of the currently visited directory. You can optionally add a text comment.cdf d1
: The name of the command was inspired by the words “Change Directory using a bookmark of the File” and switches the user to the directory that is paired with the saved bookmark. Optionally, the name of the bookmark can be supplemented with a slash (without a space) and the name of one of the actual subdirectories.- Example:
cdf d1/mySubdirectory
- Example:
ldf
: The name of the command was inspired by the words "List Directories from the File." The command prints a sorted list of all bookmarks and directories stored in the CSV file - including comments.ldf d1
: If we complete the previous command with the name of the bookmark, we get the path of the paired directory. In Linux, this expression can also be used, for example, to copy files. The following example copies all files with the extension in Linux Java from the bookmarked directoryd1
to the bookmarked directoryd2: cp $(ldf d1)/*.java $(ldf d2)
.
Performance Note
Although using a Java class directly may resemble interpreting a script, there is always compilation going on in the background. On newer computers, there will be a barely perceptible delay, but on older computers, such a delay can become annoying. To eliminate this problem, the class can compile itself and assemble the result into an executable JAR file, which reduces the execution time by an order of magnitude. Starting Java with only the kernel module parameter had no measurable effect on the result.
Commands for the Character Terminal
Next, I will present a description of selected commands for the character terminal. Depending on the circumstances, it may be necessary to add a path to the files. In the following examples, the first expression represents an executable of a Java file, the second denotes a class with an implementation, and the third expression represents an execution statement. Of course, you can prepare a command shortcut for each of the listed commands using a script or shell function.
java DirectoryBookmarks.java i
: (The character of the execution command is a derivation from theEnglishIntegrate
.) It prints the code needed to initialize the shell before first use. On Ubuntu, the generated code for Bash can be written at the end of the file.bashrc. On Windows, the class generates functions for PowerShell. A template for integration with the type terminal can also be found in the project directory CMD. To be sure, I remind you that the above shortcuts can only be used after introducing the generated functions into the shell; for example, by reopening the character terminal.java DirectoryBookmarks.java b
(Bookmarks): Lists all bookmarks assigned to the current directoryjava DirectoryBookmarks.java r d1
(Remove): Command removes the bookmark specified by the expressiond1
, which will of course leave the referenced directory unchangedjava DirectoryBookmarks.java f
(Fix): Removes all bookmarks that lead to non-existent (or invisible) directoriesjava DirectoryBookmarks.java c
(Compile): Compiles the source code of the current class into a JAR file; The JAR version needs to be launched with a slightly different command, see below.java -jar DirectoryBookmarks.jar i
(Integrate): Pro JAR version of the program, a slightly different initialization code isgenerated.java DirectoryBookmarks.java
; running without an "execution command" lists the current version and the list of available parameters.
Final Thoughts
Let's not get confused by the fact that the class is stored in a Maven-type project. One reason is the ease of running JUnit tests. The application was released under a license Apache License, Version 2.0, and needs Java 17 or higher to run.
Download the Source
Opinions expressed by DZone contributors are their own.
Comments