Is there an equivalent of cd - for cp or mv?
In Bash and similar shells cd -
changes the current directory to a previously visited one. It's often very handy. I wonder if there is similar shorthand for copying or moving files, like:
~/project-a/ $ cd ../project-b
~/project-b/ $ cp Makefile LICENSE - # this won't work, hence the question
I'd be also happy with zsh
specific answers.
If your shell has cd -
, then it will likely have either the special variable $OLDPWD
and/or the shortcut ~-
for the directory you've been in previously.
cp Makefile LICENSE "$OLDPWD/"
cp Makefile LICENSE ~-
cat ~-/Makefile
Indeed the POSIX shell language (upon which ksh/bash/zsh are built) specifies that cd -
should be equal to cd "$OLDPWD"
.
You can always use shell backquotes.
They act like a subshell : the command in the backquotes is executed first, and its output is placed as argument of the main command.
~/folderA$ cd ../folderB
~/folderB$ cp Makefile `cd -`
# gets expended to "cp Makefile ~/folderA"