views:

26

answers:

2

Often I want to exchange two folders like this:

$ mv a{,-alt-tmp}
$ mv a{-alt,}
$ mv a-alt{-tmp,}

i.e. the folder "a" becomes "a-alt" and "a-alt" becomes "a". Is there some bash command to accomplish this? There should be a minimal time gap between the two changes.

I could do something similar with

$ mv a{,-old} && mv a{-new,}

but this doesn't work with a real exchange of names, since one folder has an other name afterwards.

+1  A: 

Ideally you would set up symlinks so that the swaps are quick.

Since you can't hard-link directories, i imagine the move solution is the only real alternative.

foobar
The use of `mv` is also quick as long as the source and destination is on the same filesystem.
ar
+2  A: 

I'm not sure that you can do this atomically, but the time gap between three calls to mv in a small script or bash function will be small and likely sufficient for most people's needs. eg.

function altdir { mv $1 $1-alttmp; mv $1-alt $1; mv $1-alttmp $1-alt; }

Be careful, as if there is an open file handle to a file in the a/ directory, after the moving the handle will point to the file that is now in the a-alt/ directory.

ar
The last statement depend on which os you are :)
mathk