Is there an "undo" command on the Unix comand line?

8

2

I get the feeling I'm going to need it one day, like if I've just deleted something I shouldn't have, or renamed something and can't remember what it should be called, etc.

Kirt

Posted 2010-10-03T02:51:20.940

Reputation: 5 119

No OS that I know of supports such a feature. To be able to support undo for a rm -Rf / would require that there be a complete backup of the drive in existence somewhere. This is never going to be possible (or desired due to the immense time maintaining these types of backups would take). That's just one thing that one program could do; never mind undo for something like mke2fs /dev/hdb1. – krowe2 – 2014-10-24T21:47:06.517

Ah, I got here thinking more along the lines of undoing a paste (or text entered at the command line), e.g. http://askubuntu.com/questions/444966/bash-zsh-undo-erase-backwards-ctrlw

– Pat – 2015-07-22T19:47:28.063

Answers

11

Negative. There is no magic undo button in Linux.

RobotHumans

Posted 2010-10-03T02:51:20.940

Reputation: 5 758

3But if you screw something up with a typo, you can frequently use the command history to find out what you actually did as opposed to what you thought you did. – Velociraptors – 2010-10-03T03:00:34.590

2http://bash.org/?6899 – Corey – 2010-10-03T03:05:39.587

1good point history | grep partofwhatidid can be helpful – RobotHumans – 2010-10-03T03:05:40.710

7

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 and mv 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 and mv do two jobs: copying/moving and deleting. Not having cp and mv delete files is more in keeping with the unix philosophy (one command does one job) and less error-prone. (This is completely different from rm -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 file foo.

    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.

Gilles 'SO- stop being evil'

Posted 2010-10-03T02:51:20.940

Reputation: 58 319

0

There is not any "undo" option, only if you used the Trash feature.

To recover removed files, it really depends which file system you have.

There is:

It supports most of the major file systems.

HFS Plus

For HFS Plus (OS X) there are a few commercial solutions such as:

Read more: Recover Files From Formatted HFS+ Partition


For a complete list, check:


To avoid similar situations, please do the backups more frequently, use version control or read how to Make rm move to trash.

kenorb

Posted 2010-10-03T02:51:20.940

Reputation: 16 795