wrangler
wrangler

Reputation: 2035

Automatically add aliases to .bashrc

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?

Upvotes: 3

Views: 681

Answers (2)

anubhava
anubhava

Reputation: 785068

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...

Upvotes: 7

nhed
nhed

Reputation: 6001

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)

Upvotes: 2

Related Questions