We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,987 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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!

3
Contributors
5
Replies
5 Hours
Discussion Span
1 Year Ago
Last Updated
6
Views
Question
Answered
fatalaccidents
Light Poster
42 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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
243 posts since Aug 2007
Reputation Points: 51
Solved Threads: 35
Skill Endorsements: 0

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
Light Poster
42 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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 Posting Shark
851 posts since Feb 2011
Reputation Points: 253
Solved Threads: 155
Skill Endorsements: 7

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
243 posts since Aug 2007
Reputation Points: 51
Solved Threads: 35
Skill Endorsements: 0

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
Light Poster
42 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by Fest3er and L7Sqr

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1086 seconds using 2.69MB