101 Scripts to build a Linux Recycle Bin script _linux

Source: Internet
Author: User

It is well known that Linux does not have a recycle Bin, some people are afraid to delete the wrong things (experienced Linux administrators rarely fan this error), individuals do not recommend the Recycle Bin, but should be to develop personal security awareness. A little digress.
then back 101 scripts #15 archiving Files as They ' re removed is a script to build a Linux Recycle Bin

#!/bin/sh # NEWRM, a replacement for the existing RM command, provides a # rudimentary unremove by capability and utilizing a new # directory within the user ' s home directory. It can handle directories # of content as a individual files, and if the user specifies # THE-F flag files are re

Moved and not archived. # big Important warning:you ' ll want a cron job or something similar to keep # the trash directories.

Otherwise nothing would ever actually # to deleted from the system and you'll run out of disk space! Mydir= "$HOME/.deleted-files" realrm= "/bin/rm" copy= "/bin/cp-r" if [$#-eq 0]; Then # let ' rm ' ouptut the usage error exec $REALRM # Our shell are replaced By/bin/rm fi # Parse all options looking f or ' F ' flags= ' "while getopts" dfiprrvw "opt does case $opt in f) Exec $realrm" $@ ";;
 # exec lets us exit this script directly. * flags= "$flags-$opt";; # Other flags are for ' RM ', not US ESAC do shift $ (($OPTIND-1)) # make sure THat the $mydir exists if [!-D $mydir]; Then if [!-W $HOME];  Then echo "$ Failed:can ' t create $mydir in $HOME" >&2 exit 1 fi mkdir $mydir chmod $mydir # a little bit Privacy, please fi for Arg do newname= "$mydir/$ (date" +%s.%m.%h.%d.%m "). $ (basename" $arg ")" If [-F "$arg"]; Then $copy "$arg" "$newname" elif [-D "$arg"]; Then $copy "$arg" "$newname" fi done exec $realrm $flags "$@" # We shell is replaced by REALRM

Let's say the implementation of this script
Replace the original RM command with our MYRM script with the Recycle Bin mechanism (alias alias), the file that the script will delete moves to the hidden folder named in the Personal directory under home. Deleted-files.

And then we'll see how this script is implemented.

While getopts "dfiprrvw" opt does case
 $opt in
 f), exec $realrm "$@"; # exec lets us exit this script directly.
 * flags= "$flags-$opt";; # Other flags are for ' RM ', not US
 Esac
 done

This paragraph indicates that if the command is with the –F option, the original RM command is not entered into the Recycle Bin.

For Arg
 do
 newname= "$mydir/$ (date" +%s.%m.%h.%d.%m "). $ (basename" $arg ")"
 if [-F "$arg"]; then
 $ Copy "$arg" "$newname"
 elif [-D "$arg"]; then
 $copy "$arg" "$newname"
 fi
 Done

Processing parameters in a for loop order
Newname= "$mydir/$ (date" +%s.%m.%h.%d.%m "). $ (basename" $arg ")" Name of the file in the Recycle Bin.

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.