Make “rm” Command To Move The Files To “Trash Can” Instead Of Removing Them Completely

Human makes mistake because we are not a programmed devices so, take additional care while using rm command and don’t use rm -rf * at any point of time. When you use rm command it will delete the files permanently and doesn’t move those files to Trash Can like how file manger does.

Sometimes we delete by mistake and sometimes it happens accidentally, so what to do when it happens? You have to look recovery tools (There are plenty of data recovery tools available in Linux) but we don’t know it can able to recover 100% so, how to overcome this?

We have recently published an article about Trash-Cli, in the comment section we got an update about saferm.sh script from the user called Eemil Lgz which help us to move the files to “Trash Can” instead of deleting them permanently.

Moving files to “Trash Can” is a good idea, that save you when you run rm command accidentally but few people would say it’s a bad habit of course, if you are not taking care the “Trash Can” it might be accumulated with files & folders after certain duration. In this case i would advise you to create a cronjob as per your wish.

This works on both environments like Server & Desktop. If script detecting GNOME or KDE or Unity or LXDE Desktop Environment (DE) then it move files or folders safely to default trash $HOME/.local/share/Trash/files else it creates trash folder in your home directory $HOME/Trash.

saferm.sh Script is hosted in github, either clone below repository or Create a file called saferm.sh and past the code on it.

$ git clone https://github.com/lagerspetz/linux-stuff
$ sudo mv linux-stuff/scripts/saferm.sh /bin
$ rm -Rf linux-stuff

Create a alias on bashrc file.

alias rm=saferm.sh

To take this effect, run the following command.

$ source ~/.bashrc

That’s it everything is done, now you can perform rm command which automatically move the files to “Trash Can” instead of deleting them permanently.

For testing purpose, we are going to delete file called magi.txt, it’s clearly saying Moving magi.txt to $HOME/.local/share/Trash/file

$ rm -rf magi.txt 
Moving magi.txt to /home/magi/.local/share/Trash/files

The same can be validated through ls command or trash-cli utility.

$ ls -lh /home/magi/.local/share/Trash/files
Permissions Size User Date Modified Name
.rw-r--r--    32 magi 11 Oct 16:24  magi.txt

Alternatively we can check the same in GUI through file manager.

Create a cronjob to remove files from “Trash Can” once in a week.

$ 1 1 * * * trash-empty

Note For server environment, we need to remove manually using rm command.

$ rm -rf /root/Trash/
/root/Trash/magi1.txt is on . Unsafe delete (y/n)? y
Deleting /root/Trash/magi1.txt

The same can be achieved by trash-put command for desktop environment.

Create a alias on bashrc file.

alias rm=trash-put

To take this effect, run the following command.

$ source ~/.bashrc

To know other options for saferm.sh, navigate to help section.

$ saferm.sh -h
This is saferm.sh 1.16. LXDE and Gnome3 detection.
    Will ask to unsafe-delete instead of cross-fs move. Allows unsafe (regular rm) delete (ignores trashinfo).
    Creates trash and trashinfo directories if they do not exist. Handles symbolic link deletion.
    Does not complain about different user any more.

Usage: /path/to/saferm.sh [OPTIONS] [--] files and dirs to safely remove
OPTIONS:
-r      allows recursively removing directories.
-f      Allow deleting special files (devices, ...).
-u      Unsafe mode, bypass trash and delete files permanently.
-v      Verbose, prints more messages. Default in this version.
-q      Quiet mode. Opposite of verbose.

You may also like...

  • Eemil Lgz

    Hi, this is Eemil. Deleting the Trash, as you note in the server environment example, is easiest with the -u flag, for unsafe delete (skip trash). If you run saferm.sh with no arguments, you get the usage help:

    ~ % rm
    This is saferm.sh 1.15. Better Gnome3 detection. Will ask to unsafe-delete instead of cross-fs
    move. Allows unsafe (regular rm) delete (ignores trashinfo).
    Detects gnome better. Creates trash and trashinfo directories if they do not exist.
    Handles symbolic link deletion. Does not complain about different user any more.

    Usage: /path/to/saferm.sh [OPTIONS] [–] files and dirs to safely remove:
    -r allows recursively removing directories.
    -f Allow deleting special files (devices, …).
    -u Unsafe mode, bypass trash and delete files permanently.
    -v Verbose, prints more messages. Default in this version.
    -q Quiet mode. Opposite of verbose.

    • I have tested with “Unity” DE and working fine without any issues. Thank you so much.

  • Chris Cox

    I recommend your script work a bit differently. So, moving files works when on the same filesystem and it’s fast. Otherwise you incur a full copy. So… make the script examine each file being rm’d and mv the file to a trashcan directory established on the file system corresponding to the file being moved (create the trashcan area automatically if it doesn’t exist, likely using a root auth’d passwordless sudo command). Just some ideas.

    • Eemil Lgz

      Hi Chris, I have attempted to detect which filesystem the files are on, and ask the user to “unsafe delete” if they are not on the same filesystem as the user’s folder where .local/share/Trash is. However, I dislike extra trash cans on external media, since I do not have access to the deleted files when i detach them, and I often forget to delete trash when I detach. So, currently the script asks for unsafe delete, and if you say no, it places a copy in the local trash.
      You’re welcome to help with detecting different file systems that are properly writable by the user and that have the possibility of a separate trash, and create a PR. Thanks.

Shares
Close
Please support the site
By clicking any of these buttons you help our site to get better