Change to your Favorite directories on the bash shell by scripting ‘cd’.


[ad#Google Adsense-1]
I’m sick and tired of having to remember and ‘cd’ long paths.
I decided to create my own console based Favorites-script which makes use of a definition file which is easy to change.

A newer version has been released, please visit:
http://www.petur.eu/blog/?p=190.

“cd /home/petur/Documents/school/2010/fall/chemistry” becomes “cdf chemistry”
and “cd /var/log” becomes “cdf log”

Demonstration video:

The setup process is pretty straight forward, here we go (single user setup):

Save the following script as ~/bin/cdf

#!/bin/bash
#
# Pétur Ingi Egilsson ( petur@petur.eu )
#
# cdf (Change to favorites)
# Usage: cdf favoriteALIASES=~/bin/cdf.conf
fullpath=$(grep $1, $ALIASES|cut -d, -f2)
 
if [ ${#fullpath} -ne 0 ]
then
cd $fullpath
else
echo "Error: '$1' has not been defined in $ALIASES"
echo -n "Do you want to edit the file? (y/n): "
read editFile
case $editFile in
[yY])
if [ ! -n "$EDITOR" ]
then
# Use the nano editor because
# the EDITOR env has not been set.
nano $ALIASES
else
$EDITOR $ALIASES
fi
;;
[nN])
;;
*)
echo "Please use y,Y,n or N."
exit 1
esac
fi

 
[ad#Google Adsense-1]
Make it executable

petur@klettur:~$ chmod +x ~/bin/cdf

Save the following file as ~/bin/cdf.conf

# Definition file for cdf
#
# ATTENTION: Do not use the ~ (tilda).
# WRONG: documents,~/Documents
# RIGHT: documents,/home/petur/Documents
#
# format: name,/path/to/directoryetc,/etc
logs,/var/logs
mnt,/mnt
root,/root
var,/var
 

 

Bash scripts are executed in a subshell as child processes.
This behavior is undesirable as the bash child-process cannot tell the parent to change to the new directory.
In order to work around this you’ll need to execute the script by placing a dot-space(. ) in front of it.
If you are like me you would like to avoid having to do that so create an alias like this in .bashrc :

petur@klettur:~$ echo alias cdf=\". ~/bin/cdf\" | tee -a ~/.bashrc

Do you have any comments?

[ad#Google Adsense-1]

10 thoughts on “Change to your Favorite directories on the bash shell by scripting ‘cd’.

  1. bjd

    How about instead doing:
    cd /home/p*/Doc*/s*/2010/f*/chem*

    Usually, the longer the path, the better chance something like this expands to one unique name.

    Side note to yours and my solution: better always double check that you end up where you want to be…

    bjd

  2. cosmix

    Looks goo but, IMHO, a simpler way is to employ ~/.bashrc and define aliases like these:
    alias cdl=’cd /var/log’
    alias cdd=’cd ~/a/directory/where/I/keep/Documents/’

  3. skai

    another way to enhance your “cd” experiment is to use environnment variables that refers to the full pathes.

    ex : export LOG=/path/to/my/nices/logs

    cd $LOG and you’re done.

    just add some var defined in .bashrc for example.

    cosmix : I also use your solution for some commands, but too many commands may be complicated at lenght.

  4. Mario Vanoni

    I use always “jump”, a shell script born
    # Remember current directory, and return to that directory at later time.
    # UNIX World, March 1993, side 77ff.
    The original script is gone in nirvana.

  5. Stuart Rackham

    Nice post, I made a couple of tweaks:
    1. Made it a function in my ~/.bashrc file so there’s no need for the alias.
    2. Expanded environment vars and tildas in aliases file.

    function cdf() {
    #
    # Pétur Ingi Egilsson ( petur@petur.eu )
    # http://www.petur.eu/blog/?p=175
    #
    # cdf (Change to favorites)

    local aliasfile fullpath
    aliasfile=~/.cdfrc

    if [ $# -eq 0 ]
    then
    echo Usage: cdf FAVORITE
    return 1
    fi

    fullpath=$(grep $1, $aliasfile|cut -d, -f2)
    fullpath=$(eval “echo -n $fullpath”)

    if [ ${#fullpath} -ne 0 ]
    then
    cd $fullpath
    else
    echo “Error: ‘$1′ has not been defined in $aliasfile”
    echo -n “Do you want to edit the file? (y/n): ”
    read editFile

    case $editFile in
    [yY])
    if [ ! -n "$EDITOR" ]
    then
    # Use the nano editor because
    # the EDITOR env has not been set.
    nano $aliasfile
    else
    $EDITOR $aliasfile
    fi
    ;;
    [nN])
    ;;
    *)
    echo “Please use y,Y,n or N.”
    exit 1
    esac
    fi
    }

  6. Pingback: Change to your Favorite directories on the bash shell by scripting ‘cd’ | Pétur Ingi Egilsson

Leave a Reply