Q&A: Terminal command undo

Q You frequently suggest Terminal commands to solve problems, but do not reveal how to undo changes which occur as a result. Is there a generic Undo command?

A There is no general way of undoing Terminal commands: they are not in the GUI, and Undo as a command is only supported in the GUI of OS X.

For some commands you need to reverse the command, for instance by setting a flag to off rather than on, or similar. An example of this is customising hidden application preferences using
defaults write com.prefs.file.name OptionName -bool true
to set OptionName to true, which can be reversed using the same command with ‘false’ instead of ‘true’.

For others you need to find the command or option that does the opposite to the command that you have just entered. One way to discover opposites is to browse the man page for the command using man commandname. For many commands, they are essentially irreversible; for instance, using rm to trash files makes those that you have deleted essentially unrecoverable. That single command, just two letters, puts the files in the Trash and empties it immediately.

Updated from the original, which was first published in MacUser volume 26 issue 8, 2010.