Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

mv or cp commands both expect source and destination as arguments.

In case you want to undo the change you made, or just change the source and destination you supplied before, what is the quickest way to do this?

I thought of creating a function that takes command src dest and switching src and dest, but I was wondering if there is a better way to do this.

share|improve this question
mv bar foo isn't necessarily the inverse of mv foo bar, depending on if you overwrote an existing file, and cp bar foo definitely isn't the inverse of cp foo bar – Michael Mrozek May 24 '12 at 20:21
Yes, but I am careful to use absolute path when using this. – Forethinker May 25 '12 at 9:49
up vote 3 down vote accepted

Not a way using cp and mv, but using a feature of GNU bash with readline with the usual (emacs-like) keybindings:

Just like in emacs, you can transpose words with M-t (meta-, alt-), so if you're using bash, undoing mv file_a file_b could be as simple as pressing the up arrow and hitting M-t, which changes the above to mv file_b file_a.

(Now this isn't even a proper solution, I don't know whether this will work when the arguments to mv have spaces or other less usual, special characters. And, just like +Michael Mrozek said, it's not possible to undo cp this way. For a real undo, you also have to define exactly what you mean by "undo" (for example, what if cp overwrites an existing file? there will be no way to undo it unless you wrap cp around something that keeps backup copies!))

share|improve this answer
Exactly what I was looking for. Thank You. – Forethinker May 25 '12 at 9:52
Do you happen to know how to make the space as the delimiter? – Forethinker Aug 2 '12 at 3:12
@Prometheus, no, I have no idea. Although readline has an init file, I can't find a setting for the delimiter in the manual. – njsg Aug 8 '12 at 8:15

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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