Introductionary story

There are several memes circulating the Linux space: not knowing how to exit vim, invoking sudo to get someone to make you a sandwich, how Linux is the stuff clouds are made of, &c &c.

Recently I fell victim to my own stupidity and re-created one such meme – I managed to rm -rf {$important_directory}.

Oh, sure, I used Git, but what does Git’s history help you, if you deleted the whole repository.

Luckily, after I made some tea and calmed down, I remembered I had working backups1, so all was fine, but lessons were learnt (for the umpfth time) …and today I will tell you how to avoid this issue in the first place. (But do set up backups still!)

Enter Trash

When using graphical interfaces we are used to there being an intermediary space – the trash can / recycling bin – that we can rummage through for unintentionally deleted files. In the CLI though the machine just does what we tell it to do. Especially if we --force it to do so.

And this difference is what is the cause of mishaps – such as mine last week.

What then if rm would work like deleting things in your graphical file manager? That you could find stuff still in the “Trash bin“?

What if I told you that is exactly what Trash-CLI does? :)

Basic use of trash

In practice what you do is simply trash {$file} or trash {$folder}.

Yes, no need for --recursive and --force flags! Just one command to remember – it is that simple. And what you removed will land in the .Trash/ folder instead.

The full command list is pretty easy to understand as well:

  • trash-put, or simply trash – trash files and directories
  • trash-empty – empty the trashcan(s)
  • trash-list – list trashed files
  • trash-restore– restore a trashed file
  • trash-rm – remove individual files from the trashcan

Interacts with your Desktop

Even better than that, trash is not reinventing the wheel, but actually a CLI implementation of FreeDesktop.org’s Trash Can specification.

That means that whatever you delete with trash in the console will also be seen in the “Trash bin” in your DE or file manager. So if you use a DE or file manager that already makes sure your “Trash bin” does not fill up your whole disk, the very same rules will apply automatically to what you removed with trash.

Vice-versa, in the CLI trash-list will show also all the files deleted in your graphical file manager.

You can even do cool things like first removing all the temporary files from the trash bin with trash-rm '*~'2 and sifting through the rest more easily in the graphical file manager of your choice.

Should I make rm an alias for trash?

I was thinking about that as well, and decided against it. While trash can parse (or rather take and ignore) the most common rm flags, I think it is a bad idea for completely non-technical reason.

Imagine you have rm as an alias for trash and get used to continue to use rm for everything. Then on another machine you do not remember to (or cannot) install trash, and your continue to think rm sends files to .Trash/. You will be even more careless with rm relying on a safety net that might, or might not be there.

Instead I warmly recommend to just re-train your muscle memory to trash files, and use rm extremely sparringly. rm is a potentially dangerous command, and we should treat it with due respect.

If you are still not convinced, see these two FAQ entries in the projects own README file.

Get Trash!

I hope I convinced you to do the sane thing and start using trash as your primary means of deleting files, so we can all avoid the pain and frustration of an accidental rm -rf.

Take note though, that there are several packages with a similar name. At least on Arch/Manjaro and Debian/(K)Ubuntu the package I described above is called trash-cli.

Hat-tip to Cleydyr Albuquerque for telling me about this gem.

Also, if you have not yet – set up backups! Like right after you have installed trash-cli.

hook out → I use Arch, BTW …well, actually Manjaro, but close enough for this joke OJO


  1. For several years now I use Borg backup and am very happy with it. I might write about it some day. 

  2. You need to escape the special characters, so the globbing gets done by trash and not by the shell itself already. 


Related Posts


Published

Category

Tehne

Tags