Is there an "undo" command on the Unix comand line?
Solution 1:
Negative. There is no magic undo button in Linux.
Solution 2:
No.
Unix doesn't natively provide an undo feature. The philosophy is that if it's gone, it's gone. If it was important, it should have been backed up.
Instead of removing a file, you can move it to a temporary “trash” directory. Some desktop environments make their “delete” command move the file to the trash. But beware that only a few applications will use it; others will just remove the file.
Many commands can be reversed, e.g., a file move can be undone by moving the file back. Commands that delete or overwrite a file can't easily be undone if at all; some of them can be made more robust against accidental data loss through shell settings. In particular:
-
On the command line,
cp
andmv
will overwrite any existing target file, but you can make them prompt:alias cp='cp -i' alias mv='mv -i'
I recommend these two aliases because without them,
cp
andmv
do two jobs: copying/moving and deleting. Not havingcp
andmv
delete files is more in keeping with the unix philosophy (one command does one job) and less error-prone. (This is completely different fromrm -i
, which asks for confirmation before doing its job — such a systematic prompt tends to trigger an automatic response and thus becomes a useless annoyance.) -
You can make the
>
redirection operator only accept to create files, so that you have to write>|foo
to allow overwriting an existing filefoo
.set -o noclobber
There is a FUSE filesystem that automatically keeps copies of old versions: copyfs, available for most unices (*BSD, Linux, OS X, Solaris). Of course, that can use a lot of resources.
If you've moved a file to a different directory and remember (part of) its name but not its location, you can use the locate
command to find it.
The best way to protect against such accidents is to use a version control system (cvs, bazaar, darcs, git, mercurial, subversion, ...). It takes a little time to learn, but it pays off awesomely in the medium and long term.