开发者

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?


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


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜