Skip to main content
Tools

I Created a Better Way to Navigate Than the Classic cd Command

LHB community member Guillaume shared why he created an alternative to the cd command and how it works.

LHB Community

Warp Terminal

Anyone who works in a terminal, Linux or Windows, all the time knows that one of the most frequently used Linux commands is "cd" (change directory).

Many people have come up with tools to change the current directory intuitively. Some people use the CDPATH environment variable while some go with zoxide, but which doesn't suit my needs.

So I created a tool that works for me as a better alternative to the cd command.

Here's the story.

Why did I build a cd command alternative?

In my daily work, I've used the cd command a few dozen times (that's about the order of magnitude). I've always found it annoying to have to retype the same paths over and over again, or to search for them in the history.

By analyzing my use of “cd” and my command history, I realized that I was most often moving through fifty or so directories, and that they were almost always the same.

Below is the command I used, which displays the number of times a specific directory is the target of a “cd” command:

history | grep -E '^[ ]*[0-9]+[ ]+cd ' | awk '{print $3}' | sort | uniq -c | sort -nr

Here's how it works step by step:

  1. history: Lists your command history with line numbers
  2. grep -E '^[ ]*[0-9]+[ ]+cd ': Filters only lines that contain the cd command (with its history number)
  3. awk '{print $3}': Extracts just the directory path (the 3rd field) from each line
  4. sort: Alphabetically sorts all the directory paths
  5. uniq -c: Counts how many times each unique directory appears
  6. sort -nr: Sorts the results numerically in reverse order (highest count first)

The end result is a frequency list showing which directories you've changed to most often, giving you insights into your most commonly accessed directories.

The above command won't work if you have timestamp enabled in command history.

From this observation, I thought, why not use a mnemonic shortcut to access the most used directories.

So that's what I did, first for the Windows terminal, years ago, quickly followed by a port to Linux.

Meet cdd

Today cdd is the command I use the most in a console. Simple and very efficient.

GitHub - gsinger/cdd: Yet another tool to change current directory efficiently
Yet another tool to change current directory efficiently - gsinger/cdd

With cdd, you can:

  • Jump to a saved directory by simply typing its shortcut.
  • Bind any directory to a shortcut for later use.
  • View all your pre-defined shortcuts along with their directory paths.
  • Delete any shortcut that you no longer need.
0:00
/1:01

Installing cdd

The source is available here.

The cdd_run file can be copied anywhere in your system. Don't forget to make it executable (chmod +x ./cdd_run)

Because the script changes the current directory, it cannot be launched in a different bach process from your current session. It must be launched by the source command. Just add the alias in your ~/.bashrc file:

alias cdd='source ~/cdd_run'

Last step: Restart your terminal (or run source ~/.bashrc).

Running cdd without argument displays the usage of the tool.

In the end...

I wanted a short name that was not too far from "cd". My muscle memory is so used to "cd" that adding just a 'd' was the most efficient in terms of speed.

I understand that cdd may not be a tool for every Linux user. It's a tool for me, created by for my needs, and I think there might be a few people out there who would like it as much as I do.

So, are you going to be one of them? Please let me know in the comments.

CTA Image

Guillaume Singer works as a software architect, developer, and tech lead in the healthcare industry (for over three decades). His interests are operating systems in general, Linux in particular, and software development (C, C++, C#, Python, Erlang, Go, Rust)

LHB Community