Given the correct values in $A and $B, that should not be very hard. However, if this needs to happen in a loop, cd:ing back so that the next iteration of the loop works right is a bit of an issue, especially if there may be symbolic links involved. Probably the easiest is to grab pwd into a variable, and cd back to that location at the end of the loop. Another solution is to run each loop iteration in a subshell; the parent's working directory will remain the same throughout. (Hint: use round parentheses to run commands in a subshell.)