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 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. Script is hosted in github, either clone below repository or Create a file called and past the code on it.

$ git clone
$ sudo mv linux-stuff/scripts/ /bin
$ rm -Rf linux-stuff

Create a alias on bashrc file.


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, navigate to help section.

$ -h
This is 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/ [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.

You may also like...

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