CD Linux command on steroids!


I have seen a similar implementation of a CD command for bash/linux that used a method to store a kind of DIR bookmarks and i really liked it. For a while i used it, but because it was two seperate aliases and very simple i decided to make my own.

So this is my implementation of an alternative CD command, that can store directory bookmarks, with aliases and even use it as the normal CD command. The cool thing with this implementation is the use of aliases/names for the stored directory bookmarks and that it has multiple levels of usage… lets explain.

At first, you can use it as the simple cd command. Just give the name of the script with a directory location and you will go into that dir.

When you are inside a directory that you want to store/add a bookmark, just give the name of the command ex. cdb like this:

cdb -s

All bookmarks are stored in your home directory inside the .dirbk.csv file. You can alter this, from inside the script, if you want.

To change to a stored directory just give the command with the name of the bookmark you gave before. ex:

cdb vids

If you mistyped the alias/name of the bookmark and the script didn’t find a valid directory, it will use FZF to display all stored bookmarks and chose from a list! Neat ha?…

You can edit or view the bookmark file just with a parameter in the command, so you don’t have to remember the location of the file. Just give: cdb -l to view the file or cdb -e to edit it, with NANO. You can also see the location of the file with this syntax:

cdb -f

It’s nothing too complicate but i think that is very useful. I prefer to add a specific character in front of my aliases, just to be sure that i am accessing a bookmarked dir and not an actual dir with the same name. For example to store my Videos dir. i use an alias like xvideos. In this case there is not a chance to navigate to a directory named Videos, perhaps in another place of my hard drive, but the specified one in the bookmark.

To get the script, visit the Files area in the website and get the file named “cdb”. Copy the script inside a folder that is in your PATH enviroment variable and make sure to make it executable. As a last step, create a Linux alias command like this, which i prefer to use:

alias c=". cdb"

So, when i want to use it i type something like this:

c -s name

Make sure to open the script and read the instructions.

#!/bin/bash

#title           : Alternative CD command
#description     : A CD command that stores bookmarks and uses aliases for directories
#author          : XQTR
#date            : 20240701
#version         : 1.0
#usage           : See below
#notes           : Another Droid BBS // andr01d.zapto.org:9999

# To use it create an alias like this:
# alias cc=". cdb"
# Do not use "cd" or "cdb" as the name of the alias or it will crash.

DIR="/home/x"
BK=$DIR/.dirbk.csv

usage() {
  echo ""
  echo "Usage:"
  echo "$0 -s <name>: Stores current dir. with the given name/alias"
  echo "$0 -e       : Edit bookmark file"
  echo "$0 -l       : List bookmark file"
  echo "$0 -f       : Echo bookmark filename"
  echo "$0 <dir>    : Changes to the given dir/alias"
  echo ""
}

if [ $# -eq 0 ]; then
  cd
else
  if [ $1 == "-s" ]; then
    if [ $# -eq 2 ]; then
      echo "$2;$(pwd)" >> $BK
    else
      usage
    fi
  elif [ $1 == "-l" ]; then
    less $BK
  elif [ $1 == "-e" ]; then
    nano $BK   
  elif [ $1 == "-f" ]; then
    echo $BK   
  elif [ $1 == "--help" ]; then
    usage
  else  
    cd "$@" >/dev/null 2>&1
    if [ $? -ne 0 ]; then
      ans="$(grep "$1" $BK | cut -d";" -f1)"
      if [ ! -z $ans ]; then
        cd "$(grep $1 $BK | cut -d';' -f2)"
      else
        ans=$(cat $BK | fzf)
        if [ ! -z $ans ]; then
          cd "$(echo $ans | cut -d';' -f2)"
        fi
      fi
    fi
  fi
fi

Add a comment

Previous Next