Register
It is currently Tue Sep 16, 2014 11:27 am

sd - easily store dir paths for cd'ing/insertion/etc


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Tue Apr 06, 2010 10:29 am   

Joined: Wed Mar 10, 2010 8:05 pm
Posts: 25
add this alias to you .bashrc and .bash_profile for this to work properly, otherwise it wont change dirs for you:
alias sd=". sd"

Code:
#!/bin/bash

sdfile="$HOME/.stored-paths"

function delete_entry {
   newfile=""
   firstline=1;
   while read line; do
      i=`echo $line | cut -d\  -f1`
      if [[ "$i" != "$1" ]] ; then
         if [[ "$firstline" == "0" ]] ; then
            newfile="$newfile"$'\n'
         else
            firstline=0
         fi
         newfile="$newfile""$line"
      fi
   done < $sdfile
   echo "$newfile" > $sdfile
#   eval echo "\${newfile%\$'\n'}" #> #sdfile
}

function store_entry {
   while read line; do
      i=`echo $line | cut -d\  -f1`
      if [[ "$i" == "$1" ]] ; then
         delete_entry "$i"
         break
      fi
   done < $sdfile
   echo "$1 $2" >> $sdfile
   sort < $sdfile -o $sdfile
}

function change_dir {
   while read line; do
      i=`echo $line | cut -d\  -f1`
      if [[ "$1" == "$i" ]] ; then
         dir="`echo $line | cut -f 2 -d\ `"
         cd "$dir"
         echo "$dir"
         return 0
      fi
   done < $sdfile
   echo "directory identifier not found." >&2
   return 1
}

if [[ -f $sdfile ]]; then
   echo > /dev/null
else
   touch $sdfile
fi

if [[ "$1" == "--clear" ]] ; then
   printf "" > $sdfile
elif (( $# == 0 )) ; then
   cat "$sdfile"
elif [[ "$1" == "-d" ]] ; then
   delete_entry "$2"
elif [[ "$2" != "" ]] ; then
   store_entry "$1" "$2"
elif [[ "$1" != "" ]] ; then
   change_dir "$1"
   return "$?"
elif "$1" == "-h" || "$1" == "--help" ; then
   echo "sd - store directories for quick cd'ing"
   echo "Copyright (C) Brandon Captain. Released under the GPL v2."
   echo ""
   echo "Usage: sd 1 /usr/src/linux/  # store path at identifier '1'"
   echo "Usage: sd bin /usr/bin       # store path at identifier 'bin'"
   echo "Usage: sd 1                  # cd to path at identifier '1'"
   echo "Usage: sd                    # show all paths"
   echo "Usage: sd -d 1               # clear path 1"
   echo "Usage: sd --clear            # clear all paths"
   echo "Usage: sd -h                 # this help screen"
   echo "Usage: sd --help             # this help screen"
fi


Last edited by caibbor on Tue Apr 06, 2010 4:01 pm, edited 2 times in total.

Top
 Profile  
 PostPosted: Tue Apr 06, 2010 11:02 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 546
don't dirs, popd and pushd do it?


Top
 Profile  
 PostPosted: Tue Apr 06, 2010 3:26 pm   

Joined: Wed Mar 10, 2010 8:05 pm
Posts: 25
annoyingly. why should something as simple as a list of directories be treated as things that need to be stacked? why should I have to re-push a directory after pop'ing it. that entire method seems completely ludicrous to me.

sd linux /usr/src/linux # add /usr/src/linux under identifier "linux"
sd linux # cd to /usr/src/linux
sd linux # cd to /usr/src/linux again... and again and again if desired. no push/pop B.S. (I truly hate that method)

and this is good for more things than just cd'ing to directories:

ls `sd linux` # list contents of /usr/src/linux

the directories/identifiers will remain even after a reboot, and will be there for any and every shell you open.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Yahoo [Bot] and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP