mv file dir/ && cd "$_" && pwd
This one liner allows you to move a file or directory and cd
the to the destination of the move.
The variable $_
is automatically set by Bash to contain the last argument of the previous command. In this example the last argument was dir/
, so with cd "$_"
we effectively cd
into the destination directory of the move.
Finally, the pwd
command is just to demonstrate that we are in a different directory now, as this command prints the current directory.
If you frequently move files to directories and then immediately change to the destination directory, then you may want to wrap this one-liner into a function, let's call it for example mvcd
:
mvcd() { mv "$@" && cd "$_" && pwd; }
Using this function now we can have the same effect as in the example simply with:
mvcd file dir/