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!

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
}

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!

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

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.