Shell-script: Changing to most used directories easily

10 views
Skip to first unread message

Dilawar Singh

unread,
Jun 27, 2013, 12:56:29 AM6/27/13
to wncc...@googlegroups.com
This shell script creates a database of your directory accessing activity. And uses this database to figure out most used directories during last three days. You can easily modify this script to your taste. To use it, you have to source it into your bashrc. For example, if script (named cd.sh) is stored in ~/Scripts folder then add this line at the end of your ~/.bashrc file.
 
source ~/Scripts/cs.sh

This will create a bash function 'c'. Instead for changing directory by 'cd' command, you have to use 'c'. (You can alias cd to c, though it is not recommended). Path can be passed fully to this command or a fragment of path can be passed. If nothing is given as argument, it will list the most used directories in last 3 days e.g.

dilawar@hobbes ~$
c
0 : /home/dilawar/Works/hpc21/bliff/Haskell
1 : /media/Public/DC/share/ThisIsWhatICallEntertainment/Friends/Season03
2 : /home/dilawar/Works/hpc21/verilogxml/Haskell
3 : /media/Public/DC/share/ThisIsWhatICallEntertainment/Friends
4 : /media/Public/DC/share/ThisIsWhatICallEntertainment/Friends/Season04
Give your choice [default 0] :


I can pass a fragment of path, like
dilawar@hobbes ~$
c
Haskell
bash
: cd: Haskell: No such file or directory
Search for matching ...
0 : /home/dilawar/Works/hpc21/bliff/Haskell
1 : /home/dilawar/Works/hpc21/verilogxml/Haskell
Give your choice [default 0] :


There is more than one path found matching with 'Haskell' therefore it provides a choice-list, if there was only one choice, it would simple change into that particular directory.

On technical note, command used to log entries into sqlite3 database are launched in a sub-shell. The parent shell does not slow down due to sqlite3 logging. It is as fast as cd command.

You should to install sqlite3.

--
Dilawar


Prashant Sohani

unread,
Jun 27, 2013, 5:30:13 AM6/27/13
to wncc...@googlegroups.com
I doubt if any of us really happens to frequently visit some same directories, and yet not notice the same on our own.

At work, and often for personal projects, I find that the most frequent directories would be the root directory of source of my active project(s); since most version control and compile control mechanisms work from there. And since the same remains unchanged for several weeks at the least, I find it worthwhile enough to actually add bash aliases to quick jump there.. For instance, for Xonotic I have the aliases xns xnu xnc and xnr for cd to source, update source, compile and run respectively.

But a traking script might be real handy if applied to frequent commands. Nodoubt I do run some commands more frequently than others: like apt-get update and apt-get dist-upgrade  ( I wonder why I never considered making a cron job of the same.. perhaps I like to oversee the package changes happening to the system :D ) .. and there may be many more.. I wonder if there might be some easy way to speed-dial them ; and if it's really worth the effort.





--
--
The website for the club is http://stab-iitb.org/wncc
To post to this group, send email to wncc...@googlegroups.com
 
---
You received this message because you are subscribed to the Google Groups "Web and Coding Club IIT Bombay" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wncc_iitb+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Dilawar Singh

unread,
Jun 27, 2013, 5:52:52 AM6/27/13
to wncc...@googlegroups.com
It useful when work is arranged in a way that they directories gets deeply nested. I keep all of my work in ~/Work directory (git repositories) and there are subdirectories which goes quite deep.

Once you get used to it, it is as effective as having an alias.

I know it's limited appeal like git, tex, and vim among most of us. Even for terminal addict it may have only limited appeal, but this is not the point of sharing it. Someone might modify it or may come up with a better way to do it. After all operation of changing to directory is a fundamental operation on all OS. Thanks to sqlite3 and mysql, even poor people can play with databases. 
 

Dilawar
EE, IITB

Navin Chandak

unread,
Jun 27, 2013, 3:01:45 PM6/27/13
to wncc...@googlegroups.com
Hi,
reverse-i search in terminal also serves a similar kind of purpose !

Manish Goregaokar

unread,
Jun 27, 2013, 7:22:50 PM6/27/13
to wncc...@googlegroups.com
For me I have a shortcut that ln -s's the current directory to ~ . This works just fine :)

-Manish Goregaokar

Dilawar Singh

unread,
Jun 27, 2013, 9:23:32 PM6/27/13
to wncc...@googlegroups.com
Ctrl+R to do reverse search will be more effective if all instances of terminal share their history. It can easily figure out a matching command in shell history.


Dilawar
EE, IITB


On Fri, Jun 28, 2013 at 12:31 AM, Navin Chandak <navinch...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages