Sunday 23 December 2012

Active Directory

Let me just write about a few commands in Linux that I think are quite useful. What do you do if you have several files in different directories that you want to edit or move around?

One way is of course to just open different windows and click your way around. But this is kind of slow, especially when you have to switch between the mouse and keyboard frequently. And it tends to give you cramps in your shoulder.

Alternatively, you can open several shells and go to the different directories. Type pwd to get the path of one directory and copy and paste it into a different shell window with your mouse. Same problem: too much switching between keyboard and mouse.

My suggestion is the following, you just define a few aliases:  
alias adir='unlink ~/general/ADIR; ln -s `pwd` ~/general/ADIR; pwd >> ~/temp/adir.txt'
alias cda='cd -P ~/general/ADIR' 
alias shad='tail ~/temp/adir.txt' 

Now you can simply use the command adir to mark a directory as active. If you want to go to the same directory from a different shell, simply type cda. shad shows you a list of the most recent active directories defined, which helps you quickly find the directories you were working in recently.

For any other thing you want to do, you can of course simply use the link

Aside from that I have a little batch script called cpa:

cp -i $@ ~/general/ADIR

This copies to the active directory whatever you give it as an argument, e.g. simply
cpa inpfile.txt

In the advanced version you can do this between different computers. Let's say, I first define an active directory in a shell logged into a remote server using adir. Then I could use the following script to copy things there from my local machine, scpva

scp $@

Or you can combine it with sshfs. For example from my private machine I can execute the following command

alias cdav='cd ~/vsc/`tail -n 1 ~/vsc/temp/adir.txt | cut -d / -f 5-`'

This will cd to the place where the active directory on my remote server is mounted locally.


SimonBennie said...

Another good one is "ctrl-r". A recursive bash search of your history

"cd -" return to previous working directory

And the very powerful "!$" which allows one to use the last argument of a previous command in a new command. For instance
mkdir I-like-my-basis-sets-polarised
cd !$

Felix said...

"Ctrl+R" is a good one. Someone showed me a few months ago, and I am using it all the time since ...

the other ones also look nice