Can we undo operations done in a terminal, for example, file deletion through rm?
Solutions obtained:
- Aliasing
- Undelete utilities
- Backup utilities
- LibTrash
- Versioning (FUSE)
Can we undo operations done in a terminal, for example, file deletion through rm?
Solutions obtained:
- Aliasing
- Undelete utilities
- Backup utilities
- LibTrash
- Versioning (FUSE)
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 rm.
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 y immediately
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.
Beware that
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.
There is no recycle bin for the command line.
You could try some of the various undelete utilities, but there's no guarantee that they would work.
Two more technical solutions have not be named yet:
You could make rm an alias for the trash command (You will need to install trash first.)
Add this to your ~/.bashrc :
alias rm='trash'
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.
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
Warnings:
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 rm.
Be extra careful and don't trust too much in rm -i, 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.
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
In the end, there has to be a command to ultimately delete a file. rm is that command!
If you want to do something else, I don't recommend aliasing rm itself, but changing your habits. Aliasing rm can have unforeseen side effects in scripts etc. But the worst side effect is probably that you condition yourself in using rm casually because you're getting used to that it can be undone, or that you get a confirmation only to realize that this didn't happen when you work on a different system!
Among the habits you can acquire, I'd put this on the list:
And of course, always think twice before actually doing an rm.
This github project might be helpful. Suppose you did that
rm very_important_file
from the terminal. Recovering this file is a tedious and not always successful process.
Instead, if you had previously used the script mentioned up, you wouldn't have to worry about this because
rm very_important_file
mv very_important_file ~/.Trash/
are equivalent.
The script handles more cases and doesn't alter your system rm at all, it's put into the user local bin folder and shadows the system rm yet without affecting or disabling it.
So you can consider this a refined aliasing approach but without losing any features.