Shell: The easiest way to prevent from accidental file deletion on Fish shell ver. 4

Last Updated on February 9, 2022 by shibatau

I. What’s the problem?

I have deleted files with the rm command in the terminal and couldn’t recover lost the important files a few times. I have found a good resolution to avoid accidentally removing important files here:

How to prevent and recover from accidental file deletion in Linux

Ok, that’s good idea. I use Mac and Fish shell but I will create Mytrash for a trash folder and move files to the folder instead of removing them with rm. But isn’t it annoying to write mv FILE to ~/Mytrash? Yes, I should create a function for moving a file.

You can learn more about Fish here:

II. Move files to Mytrash with a simple function on Fish

Create a trash directory.

mkdir Mytrash 

Create a function to move a file to Mytrash directory

function Mytrash
   mv "$argv" ~/Mytrash

Create a sample file

touch a sample1.txt 

Move the file to Mytrash with the function created

Mytrash sample1.txt 

III. Trash files with trash-cli

It reads:

trash-cli trashes files recording the original path, deletion date, and permissions. It uses the same trashcan used by KDE, GNOME, and XFCE, but you can invoke it from the command line (and scripts).

It provides these commands:

trash-put 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.

You can learn how to install trash-cli here:

Not completed yet

IV. Select files using tab completion

It would easy to select a file with Tab Completion.

Not completed yet.

About shibatau

I was born and grown up in Kyoto. I studied western philosophy at the University and specialized in analytic philosophy, especially Ludwig Wittgenstein at the postgraduate school. I'm interested in new technology, especially machine learning and have been learning R language for two years and began to learn Python last summer. Listening toParamore, Sia, Amazarashi and MIyuki Nakajima. Favorite movies I've recently seen: "FREEHELD". Favorite actors and actresses: Anthony Hopkins, Denzel Washington, Ellen Page, Meryl Streep, Mia Wasikowska and Robert DeNiro. Favorite books: Fyodor Mikhailovich Dostoyevsky, "The Karamazov Brothers", Shinran, "Lamentations of Divergences". Favorite phrase: Salvation by Faith. Twitter: @shibatau

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.