Skip to content

Instantly share code, notes, and snippets.

@carlosmcevilly
Last active June 14, 2020 03:34
Show Gist options
  • Save carlosmcevilly/2295868 to your computer and use it in GitHub Desktop.
Save carlosmcevilly/2295868 to your computer and use it in GitHub Desktop.
bash function to cd to work/<year>/ directory
# Navigate top-level work/year project organization directories
#
# Setup:
# Put or source function in .bashrc
# Also set WORK_ROOT to point to a container directory such as ~/work
# Make a directory under that for at least the current year, e.g. ~/work/2020
#
# Usage examples:
# $ work # cd to the current year
# $ work -- # cd two years back. Or --- for three years, etc.
# $ work 2018 # cd to work/2018/
# $ work current # cd to work/current/ (special case)
# $ work l # cd to work/local/ (special case)
function work () {
arg=$1
if [[ "$WORK_ROOT" == '' ]]; then
echo must set WORK_ROOT
else
if [[ "$arg" != '' ]] && [[ "$arg" != *[^0-9]* ]]; then
# argument is numeric; assume it's a year
cd $WORK_ROOT/$arg;
else
year=`date +%Y`;
if [[ "$arg" == 'local' || "$arg" == 'l' ]]; then
year="local";
elif [[ "$arg" == 'c' || "$arg" == 'cur' || "$arg" == 'current' ]]; then
year="current";
elif [[ `echo -n "$arg" | cut -c1` == '-' ]]; then
# each dash in arg goes back a year. e.g. --- (length 3) puts us 3 years back
year=`echo $(($year-$(echo -n $arg|wc -c)))`
else
echo "use 'cur' or 'c' for current projects"
fi;
cd $WORK_ROOT/$year;
fi # non numeric arg
fi # WORK_ROOT set
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment