The implementation of Shell programming Windows Recycle Bin function sharing _linux Shell

Source: Internet
Author: User
Tags mkdir rand

I. Introduction to the function
1. Put the deleted files in the Recycle Bin
2. Recovery of deleted files
3. Implementation of the Linux RM command function, using almost the Linux system with the RM, command exactly the same
4. New features: Rm-l, RM-E, rm-c
5. The script checks the sum of the file size in the $home/.trash directory at run time, if
exceeds the maximum capacity, the script automatically returns the first half of the files recorded in the log file from the
Clearing in the receiving station, so it is recommended to delete large files (as opposed to the maximum capacity of the Recycle Bin) directly
Command/bin/rm instead of RM.

Two. How to use:
1. Place the trash file in the $HOME/bin/
2. Add the alias rm= "$HOME/bin/trash" to the $HOME/.BASHRC file, and log back on to the terminal or execute bash commands.
3. Execute command RM-E Configure the maximum capacity of the Recycle Bin, Unit K
4. The default directory for the Recycle Bin is: $HOME/.trash, the default profile is: $HOME/.trash/trash.conf
The default log file is: $HOME/.trash/trash.log
5. How to recover Documents:
Enter RM-L in the Linux terminal and then type the line ID to delete the file in RowNumber: 988 type y/y and then press ENTER to restore success.
If you want to view only the delete list, press ENTER directly after you type Rm-l or type q/q
6. More detailed parameter Introduction Please type: RM--help

Three. Matters needing attention
1. To manually empty the $home/.trash directory requires the/BIN/RM command, please do not try to use the Rm-r $HOME/.trash method.
2. The script does not support Rm-r-F, rm-rfi (option combination is more than 2) format.
3. If you can, you can even use the script as a backup script, if you want to back up test2.txt you only need to perform RM test2.txt, of course, if you really want to back up a file, it is best to write a special backup script.

Copy Code code as follows:

#!/bin/bash
#配置回收站最大的存储空间 (bytes)
#maxmemory =51200 (50M)
#maxmemory =102400 (100M)
#maxmemory =512000 (500M)

#根据情况设置为50M (Sufficient for ISOA service development)
maxmemory=3145728

#设置回收站所在的目录
Trash= $HOME/.trash
#设置日志文件所在的目录
mvlog= $trash/trash.log

From1=$1
From2=$2

Var_pwd=
Var_father=

#回收站若不存在, the new
if [!-e $trash];then
Mkdir-p $trash
chmod 755 $trash
Fi

#产生7位的随机数
function rand ()
{
A= (0 1 2 3 4 5 6 7 8 9 a B c D e a B c d e F)
For ((i=0;i<7;i++))
Todo
Echo-n ${a[$RANDOM%${#a [*]}]}
Done
}

random=$ (Rand)

#文件不存在时的提示信息
function File_null ()
{
Local file=$1
echo "Rm:cannot remove ' $file ': No such file or directory"
}

#打印参数出错后的提示信息
function echo_msg ()
{
Echo-n "rm:missing operand
Try ' rm--help ' for the more information.
"
}

function Echo_msg2 ()
{
Echo-n "rm:invalid option ' $ '
Try ' rm--help ' for the more information.
"
}

#回收站管理函数
Function Deal ()
{
Local tmp=$ (mktemp/tmp/tfile. XXXXXX)
Local num=$ (($ (Cat $mvlog | wc-l)/2)
#awk-F:-v nu= $num-v trash= $trash ' {if (NR<=NU) system (' RM-RF ' trash '/' ' $ ': ' $ '); \
#else print $} ' $mvlog | Sort-o $mvlog
Awk-f:-v nu= $num-v trash= $trash ' {if (NR<=NU) system (' RM-RF ' trash ' '/' ' $ ': ' $ '); \
else print $} ' $mvlog >> $tmp
MV $tmp $mvlog
}

jug=
#目录处理函数
function Jug_cur ()
{

Local tmp=
Local dirname=$1
Local jug=${dirname/\/*/}
If ["$jug" = = "."]; Then
var_pwd=${dirname/./$ (PWD)}
Jug=0
elif ["$jug" = "..."]; Then
tm=$ (PWD)
tmp=${tm%/*}
var_father=${dirname/.. /$tmp}
Jug=1
#elif ["$jug" = "~"];then
#return 2
Else
jug=2
Fi
}

#命令不带参数时的普通文件删除函数
function Rm1
{
Local filename=$ (basename $from 1)
Local dirname=$ (dirname $from 1)

Jug_cur $dirname
If ["$JUG"-eq 0];then
Dirname= $var _pwd
elif [$JUG-eq 1];then
Dirname= $var _father
Fi

If [-D "$from 1"];then
echo "Rm:cannot remove ' $from 1 ': Is a directory"
Else
if [!-e $from 1];then
File_null $from 1
Else
echo "$dirname: $filename: $random: $ (date +%y-%m-%d.%t)" >> $mvlog
MV "$from 1" "$trash/$filename: $random"
Fi
Fi
}

#rm-I.
function rmi ()
{
Local filename=$ (basename $from 2)
Local dirname=$ (dirname $from 2)

Jug_cur $dirname
If [$JUG-eq 0];then
Dirname= $var _pwd
elif [$JUG-eq 1];then
Dirname= $var _father
Fi

If [f "$from 2"];then
Echo-n "rm:remove regular file ' $from 2 '?"
Read answer
If ["$answer" = ' y '-o ' $answer ' = ' y '];then
echo "$dirname: $filename: $random: $ (date +%y-%m-%d.%t)" >> $mvlog
MV "$from 2" "$trash/$filename: $random"
Fi
Else
if [!-e $from 2];then
File_null $from 2
Else
echo "Rm:cannot remove ' $from 2 ': is a directory"
Fi
Fi
}

#rm-F
function RMF ()
{
Local filename=$ (basename $from 2)
Local dirname=$ (dirname $from 2)

Jug_cur $dirname
If [$JUG-eq 0];then
Dirname= $var _pwd
elif [$JUG-eq 1];then
Dirname= $var _father
Fi


If [f "$from 2"];then
echo "$dirname: $filename: $random: $ (date +%y-%m-%d.%t)" >> $mvlog
MV "$from 2" "$trash/$filename: $random"
Else
if [!-e $from 2];then
:
Else
echo "Rm:cannot remove ' $from 2 ': is a directory"
Fi
Fi
}

#rm-R
function RMR ()
{
Local filename=$ (basename $from 2)
Local dirname=$ (dirname $from 2)

Jug_cur $dirname
If [$JUG-eq 0];then
Dirname= $var _pwd
elif [$JUG-eq 1];then
Dirname= $var _father
Fi


If ["$from 2" = "."-O "$from 2" = "..."]; Then
echo "rm:cannot Remove directory: ' $from 2 '"
elif [-E "$from 2"];then
echo "$dirname: $filename: $random: $ (date +%y-%m-%d.%t)" >> $mvlog
MV "$from 2" "$trash/$filename: $random"
Else
File_null $from 2
Fi
}

#rm-RF
function RMRF ()
{
Local filename=$ (basename $from 2)
Local dirname=$ (dirname $from 2)

Jug_cur $dirname
If [$JUG-eq 0];then
Dirname= $var _pwd
elif [$JUG-eq 1];then
Dirname= $var _father
Fi


If ["$from 2" = "."-O "$from 2" = "..."]; Then
echo "rm:cannot Remove directory: ' $from 2 '"
elif [-E "$from 2"];then
echo "$dirname: $filename: $random: $ (date +%y-%m-%d.%t)" >> $mvlog
MV "$from 2" "$trash/$filename: $random"
Else
:
Fi
}

#rm-ir
function Rmir ()
{
Local filename=$ (basename $from 2)
Local dirname=$ (dirname $from 2)

Jug_cur $dirname
If [$JUG-eq 0];then
Dirname= $var _pwd
elif [$JUG-eq 1];then
Dirname= $var _father
Fi


If [-E "$from 2"];then
If [-D "$from 2"];then
Echo-n "Rm:remove directory ' $from 2 '?"
Else
Echo-n "rm:remove regular file ' $from 2 '?"
Fi

Read answer
If ["$answer" = ' y '-o ' $answer ' = ' y '];then
echo "$dirname: $filename: $random: $ (date +%y-%m-%d.%t)" >> $mvlog
MV "$from 2" "$trash/$filename: $random"
Fi
Else
if [!-e $from 2];then
File_null $from 2
Fi
Fi

}

#清空回收站
function RMC ()
{
/BIN/RM-RF $trash
}

function RML ()
{
Local tmp=$ (mktemp/tmp/tfile. XXXXXX)
Clear

if [!-d "$trash"];then
mkdir $trash
Fi

if [!-F "$mvlog"];then
Touch $mvlog
Fi
line=$ (cat-n $mvlog | awk-f: ' {print $, "FileName:" $, "Time:" $ ":" $ ":" $} ")
linecount=$ (cat $mvlog | wc-l)
Echo-e "$line"
Echo
Echo
echo [$linecount] Please enter the file for want to restore (replaced)
printf "RowNumber:"
Read answer
If ["$answer" = ' Q '-o ' $answer ' = ' q '-o ' $answer ' = ' "];then
:
Else
printf "Please Confirm (y/n):"
Read Answer1
If ["$answer 1" = ' y '-o ' $answer 1 "= ' Y '];then
address=$ (sed-n "$answer" "P" $mvlog | awk-f: ' {print $} ')
filename=$ (sed-n "$answer" "P" $mvlog | awk-f: ' {print $} ')
filerand=$ (sed-n "$answer" "P" $mvlog | awk-f: ' {print $} ')
Fullname= $address/$filename
If [-E "$fullname"];then
echo "The file exist!"
Sleep 0.5
Else
Old= "$trash/$filename: $filerand"
new= "$address/$filename"
MV "$old" "$new"
#deline =$ (cat $mvlog |sed "$answer" "D" | Sort-o $mvlog)
deline=$ (cat $mvlog |sed "$answer" "D" >> $tmp)
MV $tmp $mvlog
echo "Restore Success!"
Sleep 0.5
Fi
Fi
Fi
}

function Help ()
{
Cat << ' EOF '
USAGE:RM [OPTION] ... FILE ...
Remove (unlink) the FILE (s).

-F,--force ignore nonexistent files, never prompt
-I,--interactive prompt before any removal
--no-preserve-root do not treat '/' specially (the default)
--preserve-root fail to operate recursively on '/'
-R, R,--recursive remove directories and their contents recursively
--help Display this Help and exit

By default, RM does not remove directories. Use the--recursive (-R Or-r)
Option to remove each listed directory, too, along and all of its contents.

To remove a file whose name starts with a '-', for example '-foo ',
Use one of these commands:
RM---Foo

Rm./-foo

Note ' If you ' use RM to remove a file, it's usually possible to recover
The contents of that file. If you want more assurance that the contents are
Truly unrecoverable, consider using shred.

The bugs to <bug-coreutils@gnu.org>
Eof
}

#脚本开始

#检测回收站已用存储空间, if the maximum is reached, delete the file in the previous half of the log file
mem=$ (du-s $trash |awk ' {print $} ')
If ["$mem"-gt $maxmemory];then
Deal
Fi


If ["$#"-eq 0];then
Echo_msg
Fi

If ["$#"-eq 1];then
Case "$from 1" in
-I.)
Echo_msg

-f)
Echo_msg

-r | -R)
Echo_msg

-IR|-RI|-IR|-RI|-IF|-FI|-RF|-FR|-RF|-FR)
Echo_msg

-L)
Rml

-c)
Rmc

--HELP)
Help

-*)
ECHO_MSG2 $from 1

*)
Rm1

Esac
Fi

If ["$#"-ge 2];then
Until ["$" = "]
Todo
From2=$2
Case "$from 1" in
-I.)
Rmi

-f)
Rmf

-R|-R)
RMr

-L)
Rml

-RF|-RF|-FR|-FR)
Rmrf

-ir|-ri|-ir|-ri)
Rmir

-IF|-FI)
Rmf

--HELP)
Help
Exit 1

-*)
ECHO_MSG2 $from 1
Exit 1

*)
{
Until ["$" = "]
Todo
From1=$1
Rm1
Shift
Done
}

Esac
Shift
Done
Fi

Exit

Related Article

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.