3

I know there is probably no simple way to do this, but I thought I might put this out there. I have a directory that contains a subdirectory for each customer I deal with. I'd like to be able to type that customers directory name in, anywhere on the computer, and switch to that directory. In other words:

/dir/customers/
/dir/customers/customer1/
/dir/customers/customer2/
/dir/customers/customer3/

I'd like customer1, customer2, and customer3 to all be added to my ~/.bashrc file, and whenever I create a new customer, it would update to add that as well.

Any takers?

wrangler
  • 1,995
  • 2
  • 19
  • 22

2 Answers2

7

If you add this code in your ~/.bashrc:

for i in /dir/customers/*
do 
   alias $(basename $i)="cd '$i'"
done

It will setup aliases for customer1, customer2, customer3 (all the sub directories of /dir/customers/) and every time you add a new customer (eg: customerN) it's alias customerN will be added automatically you log in.

eg: alias customer1 is cd /dir/customers/customer1 and alias customer2 is cd /dir/customers/customer2 so on...

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Thank you for the quick response. I don't know why I didn't think of a loop. Still learning I guess. Thanks a lot. – wrangler Mar 21 '11 at 00:03
  • 1
    @DrewVS: ***NO!*** It's not necessary to use `ls` and, in fact it's a bad idea! Globbing will take care of it: `for i in /dir/customers/*`. Also, `eval` is completely unnecessary. `alias $i="cd '/dir/customers/$i'"` – Dennis Williamson Mar 21 '11 at 00:36
  • @Dennis Williamson: I agree with your suggestions, edited the script, thanks a lot. – anubhava Mar 21 '11 at 00:41
  • @anubhava: Because of the single quotes and the removal of `eval`, your `alias` command will no longer work. Use the quoting as I posted in my previous comment so the variable can be expanded and if there is whitespace in the variable it will be protected. Although whitespace would cause the `alias` command to error out, it's a good habit to always quote variable names that hold filenames. – Dennis Williamson Mar 21 '11 at 00:57
  • @Dennis Williamson: Agreed and very good suggestion once again. Made the change, thanks mate. – anubhava Mar 21 '11 at 01:01
  • @Dennis Williamson: Thanks for the tips, but this does not work. Without the ls that was used before, $i returns the full path, i.e., /dir/customers, which is an illegal alias. – wrangler Mar 21 '11 at 17:37
  • @Anubhava: Your code no longer works. Returns illegal alias name. – wrangler Mar 21 '11 at 17:38
  • @DrewVS: I just edited the script to make it work again, please check. Yesterday I think after my last edit it got this issue (lesson for me to NOT to edit the working script for minor fine-tuning issues). Sorry about that. – anubhava Mar 21 '11 at 17:55
  • @Anubhava: awesome. Works great now. Thanks for taking the time to refine this. – wrangler Mar 21 '11 at 18:46
  • @DrewVS: (and anubhava) Sorry about my mistake regarding the globbing and alias names. – Dennis Williamson Mar 21 '11 at 23:43
2

An alternative worth mentioning: you can export CDPATH=/dir/customers

and then if you are anywhere and you type cd David_Johnson you would be taken to /dir/customers/David_Johnson if such a directory existed.

The benefit here is that you could have just Added David_Johnson and did not need to resource your .bashrc for this to work (I know it's not an alias, but seems to offer a bit flexibility)

nhed
  • 5,774
  • 3
  • 30
  • 44
  • Good point. This is a simpler solution, and works well. However, the draw back that I see is that you cannot tab auto-complete, and so must know the exact name of the directory. The earlier alias solution gives you that capability. – wrangler Mar 21 '11 at 17:40
  • @DrewVS: Command completion is pretty easy to customize , but if creating N aliases works for you then there is no need :) – nhed Mar 21 '11 at 18:05