Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to move a file from a directory to another and then cd into the latter in one command? Something like mv /a/file /another/dir && cd _ but without typing second time the directory path.

share|improve this question
With most shells that use the readline library, Alt-. (assuming your terminal treats Alt as Meta) is a very handy interactive keyboard shortcut to know. It runs the yank-last-arg readline command, which pastes the last argument to the previous command. It even cycles when used repeatedly. – jw013 Oct 3 '12 at 19:32
also the environment variable !$ is the last arg of the last command ran. – h3rrmiller Oct 3 '12 at 19:34
@h3rrmiller Thanks for the tip (didn't know about it)! But I can't use this in a single command :( – Gerry Oct 3 '12 at 19:43
@h3rrmiller !$ is indeed the last argument of the last command, but it is history expansion syntax, which is completely different from an environment variable. If you prefer, _ IS a shell variable in bash that corresponds to roughly the same thing. Note that history expansion is typically not enabled in non-interactive shells. – jw013 Oct 4 '12 at 14:55
@jw013 I never knew about $_ until just now. I guess I've been doing it the hard way until now. – h3rrmiller Oct 4 '12 at 17:42
up vote 6 down vote accepted

There are some functions for moving and copying files and following them to the destination directory originating from a thread on the Arch Linux boards:

# Follow copied and moved files to destination directory
cpf() { cp "$@" && goto "$_"; }
mvf() { mv "$@" && goto "$_"; }
goto() { [ -d "$1" ] && cd "$1" || cd "$(dirname "$1")"; }

You can then {move,copy} and follow a file by issuing:

mvf file /dest/dir/
share|improve this answer
out of curiosity, why did you create the goto function instead of just using cd in your cpf/mvf functions? – h3rrmiller Oct 5 '12 at 12:08
They aren't my functions; but I would imagine it was for legibility/clarity... – jasonwryan Oct 5 '12 at 18:18

in your .bashrc:

cpcd() { cp "$1" "$2" && cd "$2"; }

thats how i would do it

then you would use cpcd as you would use cp

cpcd blah dir/

and it will copy blah to dir and if that was successful it will change directories to dir/

share|improve this answer
Although I have to mod my bashrc for it to work it is nice! – Gerry Oct 3 '12 at 19:44
@Gerry: What do you mean "although" - what's wrong with modifying .bashrc? In most cases, that is the way you do stuff like this. – Emanuel Berg Oct 3 '12 at 21:59

You can do it forwards:

mv /a/file /another/dir; cd $_

Or you can do it backwards:

cd /another/dir; mv /a/file .

Your call.

share|improve this answer

Test this option, try using verbose mv command to extract the directory and then go to the.

cd "$(mv -v file directory | grep -o '/.*/')"
share|improve this answer
@jasonwryan Test this option – Jhonathan Oct 4 '12 at 13:57
This approach is not very safe or robust and doesn't even save a whole lot of typing. Avoid parsing file names and treat them as binary blobs. – jw013 Oct 4 '12 at 17:57

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.