开发者

bash trick to copy files to a previously visited directory

he开发者_开发百科re's the scenario

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.


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


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 -l +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'


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


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


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜