2

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.

  • What do you mean by "previous directory"? Previous in your history or previous in the directory tree i.e. the parent directory? And do you mean copying in Terminal or via GUI? – FloT Apr 26 at 11:42
4

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.

  • Does it work to put "$@" in quotes? – Nonny Moose Apr 26 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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