Skip to content

Instantly share code, notes, and snippets.

@anroopak
Created May 23, 2020 13:25
Show Gist options
  • Save anroopak/ae829b49242e3f358bced4326962ee15 to your computer and use it in GitHub Desktop.
Save anroopak/ae829b49242e3f358bced4326962ee15 to your computer and use it in GitHub Desktop.
ARGS_TEXT="""
Usage: mkcdaliases {folder alias name} {folder to reach}
Eg: mkcdaliases myfolder ./my-folder
This creates a command -> cdmyfolder which navigates to my-folder
"""
_mkcdaliases() {
folder_alias=$1
folder=$2
if ([ -z "$folder_alias" ] || [ -z "$folder" ])
then echo "Invalid args. $ARGS_TEXT"
else
fullpath=$(realpath $2)
no_of_occurrences=$(cat ~/.cdaliases | egrep "cd$folder_alias=" | wc -l)
echo "No of Occu: $no_of_occurrences"
if [[ "$no_of_occurrences" -eq 1 ]]
then
sed -i "/alias cd$folder_alias=/c\alias cd$folder_alias=\"cd ${fullpath}\"" ~/.cdaliases
else
echo "alias cd$folder_alias=\"cd ${fullpath}\"" >> ~/.cdaliases
fi
fi
}
alias mkcdaliases="_mkcdaliases"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment