DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • How To Build a Google Photos Clone - Part 1
  • 3 Easy Steps for a (Dev)Containerized Microservice With Jolie and Docker
  • Packaging Python Classes as a Wheel
  • Configuring Java Apps With Kubernetes ConfigMaps and Helm

Trending

  • Building an AI/ML Data Lake With Apache Iceberg
  • IoT and Cybersecurity: Addressing Data Privacy and Security Challenges
  • Cosmos DB Disaster Recovery: Multi-Region Write Pitfalls and How to Evade Them
  • DGS GraphQL and Spring Boot
  1. DZone
  2. Coding
  3. Java
  4. Multiplatform Directory Bookmarks on the Command Line

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.

By 
Pavel Ponec user avatar
Pavel Ponec
·
Apr. 29, 24 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
1.3K Views

Join the DZone community and get the full member experience.

Join For Free

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

Windows PowerShell screenshot

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 position parameterd1 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
  • 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 directory d1 to the bookmarked directory d2: 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 the EnglishIntegrate.) 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 directory
  • java DirectoryBookmarks.java r d1 (Remove): Command removes the bookmark specified by the expression d1, which will of course leave the referenced directory unchanged
  • java DirectoryBookmarks.java f (Fix): Removes all bookmarks that lead to non-existent (or invisible) directories
  • java 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 is generated.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

  • DirectoryBookmarks.java
Unix shell Directory Java (programming language) terminal PowerShell

Opinions expressed by DZone contributors are their own.

Related

  • How To Build a Google Photos Clone - Part 1
  • 3 Easy Steps for a (Dev)Containerized Microservice With Jolie and Docker
  • Packaging Python Classes as a Wheel
  • Configuring Java Apps With Kubernetes ConfigMaps and Helm

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!