$ rm Important.txt (uh oh)
xenodium.com
xenodium.com
function rm {
for file in $@; do
mv -t ~/.local/share/Trash/files/ -- "${file}"
cat <<FROG > ~/.local/share/Trash/info/"$(basename ${file})".trashinfo
[Trash Info]
Path=$(realpath "${file}")
DeletionDate=$(date "+%FT%T")
FROG
done
}What kind of ancient environment is it without a trash capability? That's pre Windows 95. How are people handling the mistake of "rm *" in a wrong folder?
Added benefit of trash-put is, you don't need "-r" to delete a folder. It's just an annoyance from user's perspective.
If you think there's a compatibility problem in a script, just use /bin/rm and stop wasting time pulling hairs on unrecoverable deletion.
"@daily trash-empty 30"
Put this in cron to purge trashes older than 30 days daily.
It is written in a compiled language and appears to be faster on my old computer.
for file; do blah done
which is equivalent to for file in "$@"; do blah done
shorter and no bug.At least with bash.
In Bash that didn't work on files with spaces or funky characters. But, I never noticed because in Zsh, my primary shell, it works fine with all filenames.
Here's an updated one which incorperates the advice from several posts to also work in Bash:
function rm {
for file; do
mv -t ~/.local/share/Trash/files/ -- "${file}"
cat << FROG > ~/.local/share/Trash/info/"$(basename
"${file}")".trashinfo
[Trash Info]
Path=$(realpath "${file}")
DeletionDate=$(date "+%FT%T")
FROG
done
}My biggest data loss incident was like 25 years ago when I ran "swapon" on /. Wouldn't recommend it. (And that will trash your Trash, probably.)
Reminds me of a time a hard drive failed on a computer I had. I had an active ssh connection, running terminal display emacs remotely, with a very important file -- with no backup copy -- in a buffer. The computer continued to function, but I couldn't access the disk at all, and I figured as soon as I closed the ssh connection, it was game over. I managed to salvage the file by cutting and pasting through my terminal emulator.
I tried using it for syncing the files on several computers at one time. Instead of adding files to the computer that was lacking them so that both computers matched, it deleted files from the computer that had them so that both computers matched.
These days I just use 'rsync' instead.
I use all the major sync providers (namely Dropbox, GDrive, OneDrive) with three computers. Have only encountered sync problem <5 times (mostly happens when I didn't open certain computer for a long time) and they all have been just having duplicate files.
I've got ZFS snapshots as well, so I'm not overly worried, but I wasn't aware that was a failure mode.
This definitely isn't a criticism of the SyncThing project, I just made a dumb error, but it is a lesson on not "testing in prod" or entrusting important data with a non-professional (such as myself).
Or, alternatively, don't. People with that alias learning to add -f to every rm invocation are also a large source of problems.
Rm should really do something more sensible on the -i switch, like displaying a summary or the files and asking for confirmation only once. Personally, I like to run "find -name file" and once I'm ok with it, I circle the command back and add -delete to it. It is much more usable.
Their shell profiles were littered with two-letter aliases, replacing quite a few common things with their proprietary [often unrelated] versions
For example, mv for moving files became mv for Mail Volume, or something
alias rm=‘rm -iv’
for years without adding -f.Much like I don’t prepend ‘sudo’ until it requires me, at which point I stop and think.
Your ‘find -delete’ trick does the same: it creates time to think between expressing yourself and evaluating the outcome. This is great.
This is becoming more and more my favorite method as well. It's easy, it's reasonably safe, it's readable, and it works well.
I got into habbit of writing my sql as
'zdelete from product where id = 123;' and THEN i remove the prefixed-z.
Too many a time i copy and pasted wrong ids or screwed up something else.
SELECT Count(*) FROM product WHERE id = 123
And if the number looks right I replace "SELECT Count(*)" by "DELETE" begin;
delete from product where id = 123;
--verify things are correct with counts, by issuing "selects", etc.
and then issue either a "rollback;" or a "commit;"... I've been happy to have followed this pattern on occasion ;) BEGIN;
DELETE FROM product WHERE id=123;
ROLLBACK;
--COMMIT;
And then I remove/comment the ROLLBACK and enable the COMMIT when I'm confident it's correct. This way I also get the number of affected rows for the statement.I do this even on the command line but it's handier in an Emacs buffer and for busy databases you then don't need to worry about other transactions and locking while you type.
Its value is nil
Specifies whether to use the system’s trash can. When non-nil, certain file deletion commands use the function ‘move-file-to-trash’ instead of deleting files outright. This includes interactive calls to ‘delete-file’ and ‘delete-directory’ and the Dired deletion commands.
This variable was introduced, or its default value was changed, in
version 23.1 of Emacs.
You can customize this variable.
---If this option doesn't work for you, you should report a bug.
cd .zfs/whatever the nearest one was, copy it, done
I'm using zfsnap which take care for creating and destroying the snapshots. This I keep 72h with hourly snaps..
The great thing about modern computers is that they have so many resources that it's okay to build more forgiving tools.
Time Machine has been a huge lifesaver as well. Ideally you should be able to mark certain folders to automatically track all file changes over time.
There's still so much room for improving user friendliness and accessibility of computer systems, but it feels like we're either stuck in the Unix box or we've gone full kiddie lockdown mode with mobile platforms.
In the spirit of this article, one could make a function to call rsnapshot any time sensitive work is being done on anything important. Extending beyond the example could be to have a "fire-drill" function that git commits all local repos to an alternate branch, runs rsnapshot, sync's the filesystem, lftp transfers any important directories to an sftp server and/or git push and powers off the device in the event the building or datacenter go up in smoke or the laptop is stolen.
ZFS is likely the canonical example of a system that makes constant snapshots easy to use and access and deal with.
BTRFS is also not bad
I have snapshots every 15 minutes, every hour, every day, every week (different retention schedules for each). And then anything dangerous there's a wrapper for "snapshot, do it, snapshot again".
It's really pretty difficult to lose anything this way, and recovery is _very_ easy. The snapshotted files are available in a magic directory named .zfs or something.
Backups are still needed of course, because if the fs shits itself (or enough redundant drives die), you'd still be in trouble.
What is it with OS manufacturers inventing clever file systems and then hiding all the features? APFS snapshots could be really useful to power users if the tooling was better. And NTFS was really modern for its time, but things like alternate streams were super hidden to the point nobody apart from malware authors used them.
Snapshot-ing upon certain operations (e.g. file/s moved to a directory) is certain possible.
This blog post covers that pattern with my tool, httm: https://kimono-koans.github.io/inotifywait/
# dd if=/dev/zero of=/dev/sdd (CTRL-C after a few seconds)
It's quick and easy and only a slip away from a wiped hard disc. On a SSD, that would probably rip through /boot (EFI) and swap and be munching on my / before I stopped it. Oh well I could restore the OS and /home should still be intact ... probably.
As I current use Arch and prior to that Gentoo for over a decade, it's probably not the worst breakage I've done to a laptop 8)
Having said that, I have forgotten that I had an ssh session open and accidentally destroyed a dev server like that.
If I really cared, I would suggest setting up udev so the relevent disk is created with DAC permissions that allow you to flash it without needing root.
Right, the udev rule idea sounds great. That should work based on model name, I don't see any other good attriute in udevadm info output right now. Unless you use the same model for important data and transferring disk images.
Saved some of my code once.
i do something similar for uncommitted changes. every time emacs saves a file, it saves a copy to a .backups/ folder with a timestamp appended, then prunes copies to the latest 50. with similar hotkeys to git-timemachine i can travel backwards and forwards through saves of the current file.
after that i backup my home folder with git and tar[1].
losing data definitely sucks. it has to happen once, and then one has to decide: never again.
rm -rf *
in my home dir. With this it's a bit better, but you could still do rm -rf .*
in your home dir and lose your hidden .trash dir too.recyclix.so is an LD_PRELOAD-able shared library intercepting file deletions and moves files to recycle bin folder.
shred or similar functions do overwrite files.
alias rm=trash