Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I often use cp/mv to copy/move a file to a different directory. My directory structure is relatively deep so commands most often look like:
cp foo.bar ../../../../dir1/dir2/dir3/foo2.bar
after copying/moving I need to change directory to the target dir. Is there an easy way to change the directory to the destination of the last action / to the "last parameter" or something like that?
(of course, one can use the bash history.. but is there a more convenient way?)
If you have certain directories (deep in the directory tree) that you access REGULARLY, you could create symlinks to them in your home directory or create a series of custom variables in your .bashrc
A few ways to do it, some more elegant than others:
If in X, work in two different terminals (or tabs if you're using a tabbable term program), one where pwd is the source, the other in the destination directory.
Do the same thing in emacs, each directory being a dired-d buffer.
Use shell variables, i.e.:
Code:
export OD=/path/to/your/orginal/files
export DD=/path/to/your/destination/files # optional if you just stay here...
cd $DD
cp $OD/foo.bar ./foo2.bar
# Do whatever you need to do...
If you have to do a lot of directory changing you could also use pushd and popd instead of cd. So, rather than typing
Code:
cd destinationdir
use
Code:
pushd destinationdir
. Pushd remembers the directory where you were, then cds into destinationdir. You can run this on all sorts of different directories, and Bash will remember your entire trail. When you want to step backwards, just type popd to go to the previous directory.
Yes it does. Turns out it is a Bash "History Expansion" "Word Designator". Something new every day! I'm going to have to drop that one in the conversation at tea break
Well thanks, that link was really nice, till now I was unaware of the power of bash
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.