I was wondering if there is a (simple) possibility to redo/reverse a command that was executed in the bash shell? (to make it undone)

Is there something similar to the ctrl+z combination to redo any action (for example in word or LibreOffice)?

  • 8
    short answer is no. some command might be reverted, but there is no such thing a generic undo.
    – Archemar
    Mar 2, 2015 at 12:15
  • 3
    as others said, no undo command so having an up-to-date backup of an entire operating system is really useful. When something goes wrong you just restore the previous state. Take a look at rsnapshot for example Mar 2, 2015 at 12:40
  • 1
    i'd like to find a way to undo the halt -p command
    – Skaperen
    Mar 3, 2015 at 11:28
  • 3
    @Joshua Posting a comment with a command that can destroy a user's data and no explanation is not acceptable behavior. It's not funny, it clutters up the site and, most importantly, it's dangerous. Do not do that again.
    – terdon
    Mar 3, 2015 at 12:12
  • 2
    I would like to undo the shutdown command lol.. can't believe such questions can also get 8 upvotes in a day!!
    – MagExt
    Mar 3, 2015 at 17:23

7 Answers 7


You should understand that bash is just an execution environment. It executes commands that you call - it's not the business of the shell to even know what the command does, you can call any executable you want. In most cases, it's not even clear what an undo would do - for instance, can you "unplay" a movie? Can you "unsend" an e-mail? What would "undo running firefox" even mean, for instance? You may close it, but bookmarks, downloads and history won't be the same.

If you run a command, it is executed, whatever it does. It's up to you to know what you are doing. Note that this doesn't mean individual commands don't have "undo"... they can - you can even write a wrapper function that does something to protect you from foolish mistakes.

For instance, mv is easily reversible by just moving the file back where it came from, unless you have overwritten something. That's why -i switch exists, to ask you before overwriting. Technically, inverse of cp is rm, unless something was overwritten (again, -i asks you about it). rm is more permanent, to try to get the files back, you have to actually do some lower-level hacking (there are tools for that). If you considered the filesystem as a black-box, it technically wouldn't be possible at all (only the details of logical and physical layout of data allows you to do some damage control). rm means rm, if you want "trash" functionality, that's actually just mv into some pre-arranged directory (and possibly a scheduled service to maintain or empty it) - nothing special about it. But you can use -i to prompt you before deletion. You may use a function or an alias to always include -i in these commands.

Note that most applications are protecting you from data loss in different ways. Most (~all) text editors create backup files with ~ at the end in case you want to bring the old version back. On some distros, ls is aliased by default so that it hides them (-B), but they are there. A lot of protection is given by managing permissions properly: don't be root unless you need to be, make files read-only if you don't want them to change. Sometimes it's useful to have a "sandbox" environment - you run things on a copy, see if it's alright, and then merge the changes (or abandon the changes). chroot or lxc can prevent your scripts to escape from a directory and do damage.

When you try to execute things in bulk - for instance, if you have a complex find command, while loop, a long pipeline, or anything like that, it's a good idea to first just echo the commands that will get executed. Then, if the commands look reasonable, remove echo and run it for real. And of course, if you really aren't sure about what you are doing, make a copy first. I sometimes just create a tarball of the current directory.

Speaking of tarballs - tarbombs and zipbombs are quite common unfortunately (when people make an archive without a proper subdirectory, and unpacking scatters the files around, making a huge mess). I got used to just making a subdirectory myself before unpacking (I could list the contents, but I'm lazy). I'm thinking about making a script that will create a subdirectory only if the contents were archived without a subdirectory. But when it does happen, ls -lrt helps to find the most recent files to put where they belong. I just gave this as an example - a program can have many side effects which the shell has no way of knowing about (How could it? It's a different program being called!) The only sure way of avoiding mistakes is to be careful (think twice, run once).

Possibly the most dangerous commands are the ones that deal with the filesystem: mkfs, fdisk/gdisk and so on. They can utterly destroy the filesystem (although with proper forensic software, at least partial reverse-engineering is possible). Always double-check the device you are formatting and partitioning is correct, before running the command.


No, there is no such thing as an "undo" in Bash, sorry. This is why you should double-check your commands or test them on unimportant files first (if that's possible). And triple-check if you're doing things as root ;)

  • 3
    Ctr + / performs an undo for certain Control commands in the bash terminal, but not all commands are undoable. Jun 25, 2017 at 18:49
  • @EricLeschinski Could you provide any reference?
    – Erathiel
    Jun 27, 2017 at 10:40
  • It's buried down in the reply-comments of this page: skorks.com/2009/09/bash-shortcuts-for-maximum-productivity In order to see it working, type some words on the terminal. Move the cursor left over a character, press Ctrl-d for delete under character, then press Ctrl + / which undoes the delete. Jun 27, 2017 at 11:15
  • 1
    Oh, but that's something fundamentally different from undoing a command. If you remove a file with rm, the file's gone, it's passed away, it's deceased, it is no more, it's an ex-file (Monty Python alert). Now its relatives weep in sorrow and you cannot undo your deed. What you are talking about is undoing typing or deleting characters and I suspect it's a feature of your terminal emulator. I've just tried it in Terminator and it works. Switching to a different console (Ctrl+Alt+F1 in my case) doesn't allow it to work.
    – Erathiel
    Jun 28, 2017 at 13:59
  • There is terminal command entry undo, and terminal command after execution undo. I came here looking for the former. Jun 28, 2017 at 14:02

No, there is no generic undo, but some consequences are more reversible than others. Take for example rm: it's essentially used for two purposes - avoiding clutter and freeing space up for new files. With a bit of luck you can retrieve the contents of the file using tools like extundelete, because only the pointer to the start of the file is overwritten when you run rm; the content is overwritten when the operating system reuses that space (which could happen milliseconds or years later). Because of this functionality, rm falls in the middle of a scale of systems being more or less destructive. On the one hand there's trash systems, which provide a way to avoid clutter with no risk of losing the data immediately (because undo is trivial). Even more conservative are write once, read many ("WORM") systems, where the data cannot be deleted or overwritten at will by the system writing to it. At the other end of the scale is shredding systems, which overwrite file contents (usually multiple times) in addition to the pointer. At that point the technology to recover your data probably doesn't even exist yet (and possibly may never exist).

So you can see how the specific tool most in use (rm) is not the only alternative, but it provides a trade-off most people seem to be comfortable with:

  • Deleting a file pointer is very fast, and contributes very little towards deteriorating the disk
  • You're less likely to run out of space on systems which are highly automated
  • Provides very modest, but non-zero, privacy protection, and conversely, recovery guarantees
  • Low complexity and therefore low chance of bugs or unexpected behaviour

If the trade-offs your tools make are unacceptable to you, have a look around. Chances are there are tools out there which make the decisions which are right for you.

  • 3
    The only part of this answer that addresses the question is the first line
    – Alvaro
    Mar 3, 2015 at 12:08
  • Just trying to explain why things are that way, from a sysadmin perspective.
    – l0b0
    Mar 4, 2015 at 11:37

Applications that provide an 'undo' feature do so by maintaining a history of actions taken and either a method of reversing the action or a snapshot of the state of an object to restore a previous state. Commands executed in a shell perform direct actions or execute commands to perform actions.

The shell itself would not have the ability to undo the actions resulting from any command or program that was run. If the command or program maintained a history and had an undo function, then perhaps that could be used, but the shell itself can't.

For example, if you run rm important_file.txt, that will then tell the filesystem driver to "unlink" the file on the filesystem. The shell doesn't perform the action, it only calls rm which does.

In the case of wanting to undelete a lost file, it may be possible to do so, as typically deleting the file doesn't actually remove it, but only removes reference to it. After a time, the contents of the file will get overwritten as other data is saved to the disk, but for a while the original file may be restored using utilities that can find and "relink" it.


No, there is not, but if, say you messed around with a root privileged directory and somehow screwed something up, than your best bet is to save all the files you can to a USB Drive or a SD Card and reset your operating system from the ISO file (if you still have it, if not you can get another one offline...). If you deleted something you can, as wraeth said, download an application to relink the file data to the filename and have the files contents saved(see this question here). For a more in depth answer, can you tell us what you did wrong?


I won't repeat what others have said, but I can see from where you are coming. My standard approach to executing "dangerous" commands has always been to echo them to see whether all variables get properly expanded or not. If you can quote some specific instances, we might be able to help further.

For example, in a situation like:

for i in $(cat /my/file); 
   echo "Now removing file $i using rm -f $i"

Like other good answers said: there is no "undo" in the shell. But in your specific use case, to undo a shutdown, you can kill the shutdown process : see https://stackoverflow.com/a/526330

You must log in to answer this question.

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