Can we undo operations done in a terminal, for example, file deletion through
- Undelete utilities
- Backup utilities
- Versioning (FUSE)
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.Sign up to join this community
There is no general "undo" for every operation in terminal. If you wish to recover a file after using
rm you will need to look into recovery software.
An option to prevent you from future mistakes is to make
aliases for alternative commands to remove files. Add them to your
~/.bashrc and get into the habit of using them instead of
alias rmi='rm -i'
If you use
rmi, you will be prompted for confirmation of future deletes.
Try to avoid developing the habit of pressing
after you issue an
rmi command, as that will defeat the purpose.
You could also move files deleted by the
trsh command in terminal to your recycle bin (on KDE and Gnome):
alias trsh='mv --target-directory="$HOME/.Trash"'
If you use
trsh, you will have a limited "undelete" capability.
trsh dir1/file1 dir2/file1
may still cause unrecoverable data loss.
You could use trash-cli if you use KDE when you run a gui. This is command line utility to delete/restore using the KDE trash facilities.
You could make
rm an alias for the
trash command (You will need to install
Add this to your
This is preferable to
alias rm='mv --target-directory=$HOME/.Trash' since ~/.Trash is NOT the trash folder for gnome. It is better IMHO to let
trash figure out where the actual trash folder is.
btw I would have posted this in a comment but I don't have enough rep.
Two more technical solutions have not be named yet:
There's a larger question here that's worth addressing. Shell commands are not chatty (they don't double check what you want), and they expect you to know what you're doing. This is fundamental to how they are designed. It's a feature, not a bug.
Some people feel macho when they use such commands, which I think is pretty silly, but it is important to understand the dangers. You can do a great deal of damage in a terminal, even if you're not root. I think you probably really just cared about
rm, but since you said "Can we undo the the operations done in terminal", I thought this was worth saying. The general answer is no, you can't.
Option 1: See Undelete Linux Files from an ext2 File System. This page points to a program, written by Sebastian Hetze of the LunetIX company, that (as the title suggests) undeletes recently deleted files from an ext2 filesystem. Example usage:
# undelete -d /dev/hdc3 -a 10
Option 2: I have rsnapshot (rsync) running on my machine which makes snapshots hourly of my selected folders. It incrementally does this every hour, 2 hours or whatever you tell CRON to do. After a full day it recycles these snapshots into one daily snapshot and after 7 days in a weekly so on and so on. This makes me able to go back in time for about a month or so for every hour! It is pretty good with disk space as it creates symbolic links to files which never changed...
Recover using grep on
/dev/partition (Linux or Unix Recover deleted files – undelete files),
grep -b 'search-text' /dev/partition > file.txt
Just a try.
There exist undelete utilities for ext2, but most other Linux filesystems are stuck in the Stone Age and don't have any advanced usability features. Sad state of affairs considering gigantic drives with enough space to never delete a file again are commonplace.
So you are stuck with three options:
Do backup regularly, for example with a command like:
rsync -axvRP --backup --backupdir=/backup/incr/$(date -I) /home/ /backup/root/
Use a version control tool such as
git for all your work. While this will not protect against against a crazy
rm -r that kills the repository, it will protect against regular troubles as you will be using
git rm not raw
Be extra careful and don't trust too much in
trash-cli and friends, as most data you will lose on the shell you will not lose by accidental
rm, but by misdirected pipes, mistyped output files, misdirected
mv and stuff, i.e., things that will overwrite your data, not just delete it.
Do all three for maximum amount of safety.
check this ... migh be helpful http://artmees.github.io/rm/
suppose you did
from the terminal. recovering this file is a tedious and not always successful process
instead if you used the script mentioned up. you don't have to worry about this because
rm very_important_file mv very_important_file ~/.Trash/
the script handles more cases and doesn't alter your system
rm at all
and that is because it's put into the user local bin folder so it shadows the system rm and yet doesn't affect it or disable using it
this is a refined aliasing approach but without losing any feature
For me (opensuse leap 42.2, NTFS pendrive) PhotoRec - also by
testdisk creators - worked :)
However it haven't recover file names.
PhotoRec is file data recovery software designed to recover lost files including video, documents and archives from hard disks, CD-ROMs, and lost pictures (thus the Photo Recovery name) from digital camera memory. PhotoRec ignores the file system and goes after the underlying data, so it will still work even if your media's file system has been severely damaged or reformatted.
I've installed it using standard repositories in openSUSE