Dynamic Shortcut Creation with Bash

One of the things that used to get me really frustrated was having to type (and remember) really long paths to my organised data. If I have an assignment in some course of mine, I have to navigate to “~/Courses/code/cs6680/assn1/” before I get any work done. From what I hear, this is a pretty common crib that a lot of others also share. Here’s my solution (download).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/bin/bash

# Shortcut Creation
LINKDIR=~/.shells/links

if [[ ! -e $LINKDIR ]]; then
    mkdir -p $LINKDIR;
fi

if [[ $1 == "-c" && $# == 3 ]]; then
    linkname=$2;
    path=$3;
    # Check if exists
    echo "$path" > "$LINKDIR/$linkname";
    echo "Link: $linkname created";
elif [[ $1 == "-r" && $# == 2 ]]; then
    linkname=$2;
    # Check if exists
    rm -rf "$LINKDIR/$linkname";
    echo "Link: $linkname deleted";
elif [[ $1 == "-l" && $# == 1 ]]; then
    for link in $LINKDIR/*; do
        echo "`basename $link` -> `cat $link`";
    done;
elif [[ $# == 1 ]]; then
    linkname=$1;
    if [ -e "$LINKDIR/$linkname" ]; then
        cd `cat $LINKDIR/$linkname`;
    else
        echo "Link: $linkname does not exist";
    fi
else
    echo "Usage: $0 -c <linkname> <path>";
    echo "       $0 -r <linkname>";
    echo "       $0 -l";
    echo "       . $0 <linkname>";
    exit 1;
fi

Usage is pretty simple, but has one caveat – to go to a link, you have to source the script file (from what I gather, that’s the only way to change the current directory by the means of a script. I hope this useful.

1
2
3
4
5
$> shortcut -c "dm3" $PWD;
$> shortcut -l;
   dm3 -> /home/teju/Courses/cs6720/assn3/
$> . shortcut "dm3";
$> shortcut -r "dm3";

A very brief explanation of the code – it creates a file for every link you create, and the file contains the path. Note it might be interesting to add more functionality to that file, like a set of scripts to run on entering the directory, or something like that.

Bad Behavior has blocked 355 access attempts in the last 7 days.