I do not know if I explained myself but I would like to know once having gone into a directory how to copy files from that directory to the previous one without explicitly writing the path. Thank you for answering.
1 Answer
The last directory you have been is stored in variable $OLDPWD
,
So you can use:
cp file(s) "$OLDPWD"
Note: Never go without the quotes even if you won't need them, because if $OLDPWD
is empty, and you have 2 files to copy (cp file1 file2 $OLDPWD
), you will overwrite file2
with file1
. If you added the quotes, you will receive an error message target '' is not a directory
.
You can put this as a function in ~/.bashrc
:
cp2oldpwd(){ cp "$@" "$OLDPWD"; }
and then use it like this:
cp2oldpwd file1 file2 file3
Note: $OLDPWD
is not kept between shell sessions. Read my question from some time ago on how to keep it.