Tool-trash-RM in Linux

Source: Internet
Author: User

Using RM to delete files in Linux will do bad things accidentally ...... For example, if you accidentally delete the system file, you may have an idea about whether to bind the RM deletion action to the garbage bin. In this case, you can find it again if you delete the System File incorrectly.

I searched the internet and found someone has done this. I have reposted it for your reference.

Original post in this http://www.webupd8.org/2010/02/make-rm-move-files-to-trash-instead-of.html

1. Install trash-cli

This package provides a command line interface trashcan
Utility compliant with the freetier top.org trash specification. it
Remembers the name, original path, deletion date, and permissions
Each trashed file.

In ubuntu, simply run this command:

sudo apt-get install trash-cli


2. Set up the script

In Ubuntu enter this in a terminal:

sudo gedit /usr/local/bin/trash-rm


And paste this in the newly opened file, then save it:

 1 #!/bin/bash
2  # command name: trash-rm
3 shopt -s extglob
4 recursive=1
5 declare -a cmd
6 ((i = 0))
7 for f in "$@"
8 do
9 case "$f" in
10 (-*([fiIv])r*([fiIv])|-*([fiIv])R*([fiIv]))
11 tmp="${f//[rR]/}"
12 if [ -n "$tmp" ]
13 then
14 #echo "\$tmp == $tmp"
15 cmd[$i]="$tmp"
16 ((i++))
17 fi
18 recursive=0 ;;
19 (--recursive) recursive=0 ;;
20 (*)
21 if [ $recursive != 0 -a -d "$f" ]
22 then
23 echo "skipping directory: $f"
24 continue
25 else
26 cmd[$i]="$f"
27 ((i++))
28 fi ;;
29 esac
30 done
31 trash "${cmd[@]}"


Then make it executable by opening a terminal and running this:

sudo chmod +x /usr/local/bin/trash-rm


3. Create an alias for "rm" to use "trash-rm"

In Ubuntu, run this in a terminal:

gedit ~/.bashrc


and enter this at the end of the file:

alias rm="trash-rm"


and save it.

Then reload bashrc by running the following command in a terminal:

bash


That's it! Now try it out by deleting files the way you always do, using "rm" and "rm -r".

Final trash-cli tips

Since you've installed the trash-cli utility, now you can use the
following commands for manipulating the trash from the command line (the
names are self explanatory):

empty-trashlist-trashrestore-trash


Update: to get this to work for "sudo rm" as well, copy wilo108's sudo wrapper in your ~/.bashrc file. Without it, this will not work when "rm" is used with "sudo"!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.