954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Simple question about going to directory you just copied to

Hey guys,

I know this is a simple question but one that I can't quite figure out. I find myself copying to a directory a lot and then wanting to go there. I don't know if bash has anything build in or I would have to create a script, but right now nothing intuitive is coming to me. I've tried googling my way through it and not turning anything up. If anyone could help me out, at least enough to point my google searches in the right direction I would be very grateful. It is just one of those things I don't know how to do that annoy me.

Thanks!

fatalaccidents
Newbie Poster
23 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

It's one of those basic things where you have to learn enough of bash to be able to assemble commands to do what you want.

Write your own command. :) Add this to your ~/.bashrc. I couldn't think of a better way to reference the last arg. It even handles the case when the last arg is not a dir. And when you're done in the dir, you can 'popd' to get back to where you were.

cpg () 
{ 
    cp "$@";
    while [ $# -gt 1 ]; do shift; done;
    [ -d "${1}" ] && pushd "${1}" >/dev/null
}
Fest3er
Posting Whiz in Training
242 posts since Aug 2007
Reputation Points: 51
Solved Threads: 35
 

Thank you for your speedy answer. I can't say that I understand fully what the code does, so I know I have a long way to go as far as shell scripting. I code didn't work the way I wanted it to though, so I assume that I didn't explain myself correctly.

I want to type the command "cp blah.txt /to/some/long/static/path" and then be able to type functionname (or whatever) to have it cd to that long path that I just copied to.

When I used your script in this manner it says it is missing an operand, because I think it is trying to copy something (?)

Thanks again!

fatalaccidents
Newbie Poster
23 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

Bash has a really convenient way to get to that argument. You can use !$ which refers to the last argument in the preceding command.
Consider:

$ pwd 
/home/me
$ cp blah.txt /some/other/location
$ cd !$
$ pwd
/some/other/location


And, since you probably only want to be there temporarily you can push the directory on the stack and pop out when you are done.

$ pwd 
/home/me
$ cp blah.txt /some/other/location
$ pushd !$
$ pwd
/some/other/location
$ popd
$ pwd
/home/me
L7Sqr
Practically a Master Poster
657 posts since Feb 2011
Reputation Points: 201
Solved Threads: 124
 

Thank you for your speedy answer. I can't say that I understand fully what the code does, so I know I have a long way to go as far as shell scripting. I code didn't work the way I wanted it to though, so I assume that I didn't explain myself correctly.

I want to type the command "cp blah.txt /to/some/long/static/path" and then be able to type functionname (or whatever) to have it cd to that long path that I just copied to.

When I used your script in this manner it says it is missing an operand, because I think it is trying to copy something (?)

Thanks again!

Well, yes, it is trying to copy stuff. I seem to have misunderstood your query. The function (copy-and-go, or cpg) I posted copies the files and then goes to the destination directory. Which is not what you wanted. Use the interactive !$ instead.

Fest3er
Posting Whiz in Training
242 posts since Aug 2007
Reputation Points: 51
Solved Threads: 35
 

Thank you both for your answers. I never knew about the !$ command so thanks L7sqr, and thanks Fest3er for giving me a code that helped me get to know bash a little better that I may use in the future as well.

fatalaccidents
Newbie Poster
23 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You