3

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
  • 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, 2019 at 11:42

1 Answer 1

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.

1
  • Does it work to put "$@" in quotes? Apr 26, 2019 at 12:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .