views:

47

answers:

5

here's the scenerio -

you are in bash

:~/dirA$cd /dirb

:/dirb$cp filex __here_i_want_trick_to_reference_dirA

example of a similar trick is "cd -", which puts you into the previously visited directory.

I want this because, in reality, the paths I am dealing with are huge and I'm looking for a shortcut.

furthermore, a trick which handles this:

:a$/cd x

:x$/cd y

etc.

:y$/cp file _ref_to_original_dir_a

I am looking for the least intrusive way to accomplish this, if the 2nd part is not doable without too many shenanigans, then it's probably not worth it for my usage.

thanks

just an update - thanks for the replies.

http://www.hccp.org/modding-cd.html

That page describes what i am choosing. it just adds the alias to the mix for the pushd solution.

+2  A: 

This should be really easy to do with pushd, and dirs.

You simply have to issue the pushd command in the directory you want to copy your files to, and use:

cp filex `dirs +1`

You can find documentation on those builtin commands here, and examples of aliases to replace cd with them here. For example:

alias cd='pushd '
alias cd-='popd'
alias cd--='popd -2'
alias cd---='popd -3'
alias d='dirs -v'
alias b='pushd +1'
tonio
yes, i use pushd/popd, but i do not by habit navigate the system with it, usually its "cd". Would you use an awk command on the dirs output to get your reference?
Gush
i suppose i could alias cd to pushd
Gush
@Gush: `dirs -N` or `dirs +N` will get the Nth entry in the list from the right or left, respectively. No need for AWK.
Dennis Williamson
http://www.hccp.org/modding-cd.htmlthat is what I am going with. thanks for the suggestion.
Gush
+4  A: 

You can try using $OLDPWD. That variable should contain the path to the last directory you were in.

Marc Bernstein
this is exactly what i thought must exist, but couldnt find it, thanks
Gush
A: 

You can write your own cd function that stores the directory you are cd'ing to then does the actual cd. This variable would be something like LAST_CD_DIR. Then you can use that in another function you call that does the cp.

Amir Afghani
I guess this is the pedagogic solution :)
Amir Afghani
+1  A: 

Well on my system (Ubuntu) there's an environment variable OLDPWD.

In case its different on your system, you should be able to find it with the following

mkdir nonsense_dir
cd nonsense_dir
cd ..
set | grep nonsense_dir

Hopefully there aren't too many instances of the string nonsense_dir in your environment and you can spot it easily.

Oh, and you probably want to remove the directory too

rmdir nonsense_dir
torak
cool trick to see how environment changes, if i thought of this i would have noticed the OLDPWD - thanks
Gush
A: 

You can use ~- instead of OLDPWD, which expands to the value of OLDPWD as explained here: http://www.thegeekstuff.com/2010/06/bash-tilde-expansion/. Reducing the number of keystrokes...

thegeek