SlideShare a Scribd company logo
: << EOF 
trash 命令替代linux rm命令实现windows回收站的功能 
一. 功能简介 
1. 将删除的文件放在回收站中 
2. 恢复删除的文件 
3. 实现linux rm命令的功能, 使用起来几乎和linux 系统自带的rm ,命令完全一样 
4. 新增功能: rm -l, rm -e, rm -c 
5. 该脚本每次在运行时候会检查$HOME/.trash 目录下文件大小之和, 若 
超过最大容量, 脚本会自动将日志文件中所记录文件中的前一半文件从回 
收站中清除,所以建议删除大文件(相对于回收站最大容量而言)直接用 
命令/bin/rm 而不要用 rm. 
二. 使用方法: 
1. 将trash文件放到 $HOME/bin/ 
2. 在$HOME/.bashrc 文件中加入alias rm=”$HOME/bin/trash”, 重新登陆终端或执行bash命令。 
3. 执行命令rm -e 配置回收站的最大容量,单位K 
4. 回收站的默认目录为:$HOME/.trash, 默认配置文件为:$HOME/.trash/trash.conf 
默认log文件为:$HOME/.trash/trash.log 
5. 怎样恢复文件: 
在linux 终端中输入rm -l, 然后 在RowNumber: 后面键入要删除文件所在的行标识:988 键入y/Y 然后按回车键 恢复成功. 
如果想只查看删除列表, 则键入rm -l 后直接按回车键或者键入Q/q 
6. 更详细的参数介绍请键入:rm --help 
三. 注意事项 
1. 想要手动清空$HOME/.trash目录需要用/bin/rm命令, 请不要尝试用rm -r $HOME/.trash 的方法. 
2. 该脚本不支持rm -r -f , rm -rfi (选项组合超过2个)格式. 
3. 如果你可以你甚至可以用该脚本作为备份脚本, 假若想备份test2.txt你只需要执行rm test2.txt, 当然如果真想备份某个文件的话, 最好编写专门的备份脚本。 
EOF 
=======================CODE======================== 
#!/bin/bash 
#配置回收站最大的存储空间(字节) 
#maxmemory=51200 (50M) 
#maxmemory=102400 (100M) 
#maxmemory=512000 (500M) 
#根据情况设置为50M(对于isoa服务开发来说足够了) 
maxmemory=204800 
#设置回收站所在的目录 
trash=$HOME/.trash 
#设置日志文件所在的目录 
mvlog=$trash/trash.log 
from1=$1 
from2=$2 
var_pwd= 
var_father= 
#回收站若不存在,则新建之 
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++)) 
do 
echo -n ${a[$RANDOM%${#a[*]}]} 
done 
} 
random=$(rand) 
#文件不存在时的提示信息 
function file_null() 
{ 
local file=$1 
echo "rm: cannot remove '$file': No such file or directory" 
file:///home/aaron/nfs/20130816/trash.sh.html 
1 of 6 08/16/2013 01:25 PM
} 
#打印参数出错后的提示信息 
function echo_msg() 
{ 
echo -n "rm: missing operand 
Try 'rm --help' for more information. 
" 
} 
function echo_msg2() 
{ 
echo -n "rm: invalid option '$1' 
Try 'rm --help' for 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"'/'"$2"':'"$3"");  
#else print $0}' $mvlog | sort -o $mvlog 
awk -F: -v nu=$num -v trash=$trash '{if (NR<=nu) system("rm -rf "trash"'/'"$2"':'"$3"");  
else print $0}' $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 $from1) 
local dirname=$(dirname $from1) 
jug_cur $dirname 
if [ "$JUG" -eq 0 ];then 
dirname=$var_pwd 
elif [ $JUG -eq 1 ];then 
dirname=$var_father 
fi 
if [ -d "$from1" ];then 
echo "rm: cannot remove '$from1': Is a directory" 
else 
if [ ! -e $from1 ];then 
file_null $from1 
else 
echo "$dirname:$filename:$random:$(date +%Y-%m-%d.%T)" >> $mvlog 
mv "$from1" "$trash/$filename:$random" 
fi 
fi 
} 
#rm -i 
function rmi() 
file:///home/aaron/nfs/20130816/trash.sh.html 
2 of 6 08/16/2013 01:25 PM
{ 
local filename=$(basename $from2) 
local dirname=$(dirname $from2) 
jug_cur $dirname 
if [ $JUG -eq 0 ];then 
dirname=$var_pwd 
elif [ $JUG -eq 1 ];then 
dirname=$var_father 
fi 
if [ -f "$from2" ];then 
echo -n "rm: remove regular file '$from2'?" 
read answer 
if [ "$answer" = 'y' -o "$answer" = 'Y' ];then 
echo "$dirname:$filename:$random:$(date +%Y-%m-%d.%T)" >> $mvlog 
mv "$from2" "$trash/$filename:$random" 
fi 
else 
if [ ! -e $from2 ];then 
file_null $from2 
else 
echo "rm: cannot remove '$from2': Is a directory" 
fi 
fi 
} 
#rm -f 
function rmf() 
{ 
local filename=$(basename $from2) 
local dirname=$(dirname $from2) 
jug_cur $dirname 
if [ $JUG -eq 0 ];then 
dirname=$var_pwd 
elif [ $JUG -eq 1 ];then 
dirname=$var_father 
fi 
if [ -f "$from2" ];then 
echo "$dirname:$filename:$random:$(date +%Y-%m-%d.%T)" >> $mvlog 
mv "$from2" "$trash/$filename:$random" 
else 
if [ ! -e $from2 ];then 
: 
else 
echo "rm: cannot remove '$from2': Is a directory" 
fi 
fi 
} 
#rm -r 
function rmr() 
{ 
local filename=$(basename $from2) 
local dirname=$(dirname $from2) 
jug_cur $dirname 
if [ $JUG -eq 0 ];then 
dirname=$var_pwd 
elif [ $JUG -eq 1 ];then 
dirname=$var_father 
fi 
if [ "$from2" = "." -o "$from2" = ".." ];then 
echo "rm: cannot remove directory: '$from2'" 
elif [ -e "$from2" ];then 
echo "$dirname:$filename:$random:$(date +%Y-%m-%d.%T)" >> $mvlog 
mv "$from2" "$trash/$filename:$random" 
else 
file_null $from2 
fi 
} 
#rm -rf 
function rmrf() 
file:///home/aaron/nfs/20130816/trash.sh.html 
3 of 6 08/16/2013 01:25 PM
{ 
local filename=$(basename $from2) 
local dirname=$(dirname $from2) 
jug_cur $dirname 
if [ $JUG -eq 0 ];then 
dirname=$var_pwd 
elif [ $JUG -eq 1 ];then 
dirname=$var_father 
fi 
if [ "$from2" = "." -o "$from2" = ".." ];then 
echo "rm: cannot remove directory: '$from2'" 
elif [ -e "$from2" ];then 
echo "$dirname:$filename:$random:$(date +%Y-%m-%d.%T)" >> $mvlog 
mv "$from2" "$trash/$filename:$random" 
else 
: 
fi 
} 
#rm -ir 
function rmir() 
{ 
local filename=$(basename $from2) 
local dirname=$(dirname $from2) 
jug_cur $dirname 
if [ $JUG -eq 0 ];then 
dirname=$var_pwd 
elif [ $JUG -eq 1 ];then 
dirname=$var_father 
fi 
if [ -e "$from2" ];then 
if [ -d "$from2" ];then 
echo -n "rm: remove directory '$from2'?" 
else 
echo -n "rm: remove regular file '$from2'?" 
fi 
read answer 
if [ "$answer" = 'y' -o "$answer" = 'Y' ];then 
echo "$dirname:$filename:$random:$(date +%Y-%m-%d.%T)" >> $mvlog 
mv "$from2" "$trash/$filename:$random" 
fi 
else 
if [ ! -e $from2 ];then 
file_null $from2 
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 $1, "FileName:"$2, "Time: "$4":"$5":"$6}') 
linecount=$(cat $mvlog | wc -l) 
echo -e "$line" 
echo 
echo 
file:///home/aaron/nfs/20130816/trash.sh.html 
4 of 6 08/16/2013 01:25 PM
echo "[$linecount] Please enter the file you want to restore (replaced with the line number)" 
printf "RowNumber: " 
read answer 
if [ "$answer" = 'q' -o "$answer" = 'Q' -o "$answer" = "" ];then 
: 
else 
printf "Please confirm (Y/N): " 
read answer1 
if [ "$answer1" = 'y' -o "$answer1" = 'Y' ];then 
address=$(sed -n "$answer""p" $mvlog | awk -F: '{print $1}') 
filename=$(sed -n "$answer""p" $mvlog | awk -F: '{print $2}') 
filerand=$(sed -n "$answer""p" $mvlog | awk -F: '{print $3}') 
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 with 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 that if you use rm to remove a file, it is usually possible to recover 
the contents of that file. If you want more assurance that the contents are 
truly unrecoverable, consider using shred. 
Report bugs to <bug-coreutils@gnu.org>. 
EOF 
} 
#脚本开始 
#检测回收站已用存储空间,如果已经达到最大值,则删除日志文件中位于前面的一半的文件 
mem=$(du -s $trash|awk '{print $1}') 
if [ "$mem" -gt $maxmemory ];then 
deal 
fi 
if [ "$#" -eq 0 ];then 
echo_msg 
fi 
if [ "$#" -eq 1 ];then 
case "$from1" in 
-i) 
echo_msg 
;; 
-f) 
file:///home/aaron/nfs/20130816/trash.sh.html 
5 of 6 08/16/2013 01:25 PM
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 $from1 
;; 
*) 
rm1 
;; 
esac 
fi 
if [ "$#" -ge 2 ];then 
until [ "$2" = "" ] 
do 
from2=$2 
case "$from1" 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 $from1 
exit 1 
;; 
*) 
{ 
until [ "$1" = "" ] 
do 
from1=$1 
rm1 
shift 
done 
} 
;; 
esac 
shift 
done 
fi 
exit 
file:///home/aaron/nfs/20130816/trash.sh.html 
6 of 6 08/16/2013 01:25 PM

More Related Content

What's hot

Unix Commands
Unix CommandsUnix Commands
Unix Commands
Dr.Ravi
 
Process monitoring in UNIX shell scripting
Process monitoring in UNIX shell scriptingProcess monitoring in UNIX shell scripting
Process monitoring in UNIX shell scripting
Dan Morrill
 
Workshop on command line tools - day 2
Workshop on command line tools - day 2Workshop on command line tools - day 2
Workshop on command line tools - day 2
Leandro Lima
 
Unix primer
Unix primerUnix primer
Unix primer
dummy
 
What's New in PHP 5.5
What's New in PHP 5.5What's New in PHP 5.5
What's New in PHP 5.5
Corey Ballou
 
MongoDB Replication (Dwight Merriman)
MongoDB Replication (Dwight Merriman)MongoDB Replication (Dwight Merriman)
MongoDB Replication (Dwight Merriman)
MongoSF
 
How to stand on the shoulders of giants
How to stand on the shoulders of giantsHow to stand on the shoulders of giants
How to stand on the shoulders of giants
Ian Barber
 
The most exciting features of PHP 7.1
The most exciting features of PHP 7.1The most exciting features of PHP 7.1
The most exciting features of PHP 7.1
Zend by Rogue Wave Software
 
Cis 216 – shell scripting
Cis 216 – shell scriptingCis 216 – shell scripting
Cis 216 – shell scripting
Dan Morrill
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
Raghu nath
 
Nouveau document texte
Nouveau document texteNouveau document texte
Nouveau document texte
Sai Ef
 
Yy
YyYy
Yy
yygh
 
ZeroMQ Is The Answer: DPC 11 Version
ZeroMQ Is The Answer: DPC 11 VersionZeroMQ Is The Answer: DPC 11 Version
ZeroMQ Is The Answer: DPC 11 Version
Ian Barber
 
HaskellとDebianの辛くて甘い関係
HaskellとDebianの辛くて甘い関係HaskellとDebianの辛くて甘い関係
HaskellとDebianの辛くて甘い関係
Kiwamu Okabe
 
Unix cheatsheet
Unix cheatsheetUnix cheatsheet
Unix cheatsheet
Dhaval Shukla
 
Character_Device_drvier_pc
Character_Device_drvier_pcCharacter_Device_drvier_pc
Character_Device_drvier_pc
Rashila Rr
 
ZeroMQ Is The Answer
ZeroMQ Is The AnswerZeroMQ Is The Answer
ZeroMQ Is The Answer
Ian Barber
 
Introdução ao Perl 6
Introdução ao Perl 6Introdução ao Perl 6
Introdução ao Perl 6
garux
 
Functional pe(a)rls: Huey's zipper
Functional pe(a)rls: Huey's zipperFunctional pe(a)rls: Huey's zipper
Functional pe(a)rls: Huey's zipper
osfameron
 
C99.php
C99.phpC99.php
C99.php
veng33k
 

What's hot (20)

Unix Commands
Unix CommandsUnix Commands
Unix Commands
 
Process monitoring in UNIX shell scripting
Process monitoring in UNIX shell scriptingProcess monitoring in UNIX shell scripting
Process monitoring in UNIX shell scripting
 
Workshop on command line tools - day 2
Workshop on command line tools - day 2Workshop on command line tools - day 2
Workshop on command line tools - day 2
 
Unix primer
Unix primerUnix primer
Unix primer
 
What's New in PHP 5.5
What's New in PHP 5.5What's New in PHP 5.5
What's New in PHP 5.5
 
MongoDB Replication (Dwight Merriman)
MongoDB Replication (Dwight Merriman)MongoDB Replication (Dwight Merriman)
MongoDB Replication (Dwight Merriman)
 
How to stand on the shoulders of giants
How to stand on the shoulders of giantsHow to stand on the shoulders of giants
How to stand on the shoulders of giants
 
The most exciting features of PHP 7.1
The most exciting features of PHP 7.1The most exciting features of PHP 7.1
The most exciting features of PHP 7.1
 
Cis 216 – shell scripting
Cis 216 – shell scriptingCis 216 – shell scripting
Cis 216 – shell scripting
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
Nouveau document texte
Nouveau document texteNouveau document texte
Nouveau document texte
 
Yy
YyYy
Yy
 
ZeroMQ Is The Answer: DPC 11 Version
ZeroMQ Is The Answer: DPC 11 VersionZeroMQ Is The Answer: DPC 11 Version
ZeroMQ Is The Answer: DPC 11 Version
 
HaskellとDebianの辛くて甘い関係
HaskellとDebianの辛くて甘い関係HaskellとDebianの辛くて甘い関係
HaskellとDebianの辛くて甘い関係
 
Unix cheatsheet
Unix cheatsheetUnix cheatsheet
Unix cheatsheet
 
Character_Device_drvier_pc
Character_Device_drvier_pcCharacter_Device_drvier_pc
Character_Device_drvier_pc
 
ZeroMQ Is The Answer
ZeroMQ Is The AnswerZeroMQ Is The Answer
ZeroMQ Is The Answer
 
Introdução ao Perl 6
Introdução ao Perl 6Introdução ao Perl 6
Introdução ao Perl 6
 
Functional pe(a)rls: Huey's zipper
Functional pe(a)rls: Huey's zipperFunctional pe(a)rls: Huey's zipper
Functional pe(a)rls: Huey's zipper
 
C99.php
C99.phpC99.php
C99.php
 

Viewers also liked

It经典图书(附免费下载地址)
It经典图书(附免费下载地址)It经典图书(附免费下载地址)
It经典图书(附免费下载地址)
Lingfei Kong
 
Beamer guide By KiJoo Kim (a.k.a. Daisyweb)
Beamer guide By KiJoo Kim (a.k.a. Daisyweb)Beamer guide By KiJoo Kim (a.k.a. Daisyweb)
Beamer guide By KiJoo Kim (a.k.a. Daisyweb)
mustainmtn
 
Python学习笔记
Python学习笔记Python学习笔记
Python学习笔记
Lingfei Kong
 
Congfigure python as_ide
Congfigure python as_ideCongfigure python as_ide
Congfigure python as_ide
Lingfei Kong
 
Kdump
KdumpKdump
Emacs presentation
Emacs presentationEmacs presentation
Emacs presentation
Lingfei Kong
 
SR-IOV Introduce
SR-IOV IntroduceSR-IOV Introduce
SR-IOV Introduce
Lingfei Kong
 

Viewers also liked (7)

It经典图书(附免费下载地址)
It经典图书(附免费下载地址)It经典图书(附免费下载地址)
It经典图书(附免费下载地址)
 
Beamer guide By KiJoo Kim (a.k.a. Daisyweb)
Beamer guide By KiJoo Kim (a.k.a. Daisyweb)Beamer guide By KiJoo Kim (a.k.a. Daisyweb)
Beamer guide By KiJoo Kim (a.k.a. Daisyweb)
 
Python学习笔记
Python学习笔记Python学习笔记
Python学习笔记
 
Congfigure python as_ide
Congfigure python as_ideCongfigure python as_ide
Congfigure python as_ide
 
Kdump
KdumpKdump
Kdump
 
Emacs presentation
Emacs presentationEmacs presentation
Emacs presentation
 
SR-IOV Introduce
SR-IOV IntroduceSR-IOV Introduce
SR-IOV Introduce
 

Similar to Shell实现的windows回收站功能的脚本

NUMOSS 4th Week - Commandline Tutorial
NUMOSS 4th Week - Commandline TutorialNUMOSS 4th Week - Commandline Tutorial
NUMOSS 4th Week - Commandline Tutorial
Gagah Arifianto
 
Unix tips and tricks
Unix tips and tricksUnix tips and tricks
Unix tips and tricks
Aleksandar Bilanovic
 
Linux Command Line
Linux Command LineLinux Command Line
Linux Command Line
Prima Yogi Loviniltra
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
Raghu nath
 
Mkscript sh
Mkscript shMkscript sh
Mkscript sh
Ben Pope
 
zinno
zinnozinno
Logrotate sh
Logrotate shLogrotate sh
Logrotate sh
Ben Pope
 
BASH Variables Part 1: Basic Interpolation
BASH Variables Part 1: Basic InterpolationBASH Variables Part 1: Basic Interpolation
BASH Variables Part 1: Basic Interpolation
Workhorse Computing
 
Yy
YyYy
Yy
yygh
 
Shell Scripts
Shell ScriptsShell Scripts
Shell Scripts
Dr.Ravi
 
Examples -partII
Examples -partIIExamples -partII
Examples -partII
Kedar Bhandari
 
Im trying to run make qemu-nox In a putty terminal but it.pdf
Im trying to run  make qemu-nox  In a putty terminal but it.pdfIm trying to run  make qemu-nox  In a putty terminal but it.pdf
Im trying to run make qemu-nox In a putty terminal but it.pdf
maheshkumar12354
 
Cod
CodCod
Fisier.txt
Fisier.txtFisier.txt
Fisier.txt
Stan Adrian
 
Unix 1st sem lab programs a - VTU Karnataka
Unix 1st sem lab programs a - VTU KarnatakaUnix 1st sem lab programs a - VTU Karnataka
Unix 1st sem lab programs a - VTU Karnataka
iCreateWorld
 
101 3.3 perform basic file management
101 3.3 perform basic file management101 3.3 perform basic file management
101 3.3 perform basic file management
Acácio Oliveira
 
Perl basics for Pentesters
Perl basics for PentestersPerl basics for Pentesters
Perl basics for Pentesters
Sanjeev Kumar Jaiswal
 
Cpsh sh
Cpsh shCpsh sh
Cpsh sh
Ben Pope
 
Phishing for Root (How I Got Access to Root on Your Computer With 8 Seconds o...
Phishing for Root (How I Got Access to Root on Your Computer With 8 Seconds o...Phishing for Root (How I Got Access to Root on Your Computer With 8 Seconds o...
Phishing for Root (How I Got Access to Root on Your Computer With 8 Seconds o...
Vi Grey
 
Bash Scripting Workshop
Bash Scripting WorkshopBash Scripting Workshop
Bash Scripting Workshop
Ahmed Magdy Ezzeldin, MSc.
 

Similar to Shell实现的windows回收站功能的脚本 (20)

NUMOSS 4th Week - Commandline Tutorial
NUMOSS 4th Week - Commandline TutorialNUMOSS 4th Week - Commandline Tutorial
NUMOSS 4th Week - Commandline Tutorial
 
Unix tips and tricks
Unix tips and tricksUnix tips and tricks
Unix tips and tricks
 
Linux Command Line
Linux Command LineLinux Command Line
Linux Command Line
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
Mkscript sh
Mkscript shMkscript sh
Mkscript sh
 
zinno
zinnozinno
zinno
 
Logrotate sh
Logrotate shLogrotate sh
Logrotate sh
 
BASH Variables Part 1: Basic Interpolation
BASH Variables Part 1: Basic InterpolationBASH Variables Part 1: Basic Interpolation
BASH Variables Part 1: Basic Interpolation
 
Yy
YyYy
Yy
 
Shell Scripts
Shell ScriptsShell Scripts
Shell Scripts
 
Examples -partII
Examples -partIIExamples -partII
Examples -partII
 
Im trying to run make qemu-nox In a putty terminal but it.pdf
Im trying to run  make qemu-nox  In a putty terminal but it.pdfIm trying to run  make qemu-nox  In a putty terminal but it.pdf
Im trying to run make qemu-nox In a putty terminal but it.pdf
 
Cod
CodCod
Cod
 
Fisier.txt
Fisier.txtFisier.txt
Fisier.txt
 
Unix 1st sem lab programs a - VTU Karnataka
Unix 1st sem lab programs a - VTU KarnatakaUnix 1st sem lab programs a - VTU Karnataka
Unix 1st sem lab programs a - VTU Karnataka
 
101 3.3 perform basic file management
101 3.3 perform basic file management101 3.3 perform basic file management
101 3.3 perform basic file management
 
Perl basics for Pentesters
Perl basics for PentestersPerl basics for Pentesters
Perl basics for Pentesters
 
Cpsh sh
Cpsh shCpsh sh
Cpsh sh
 
Phishing for Root (How I Got Access to Root on Your Computer With 8 Seconds o...
Phishing for Root (How I Got Access to Root on Your Computer With 8 Seconds o...Phishing for Root (How I Got Access to Root on Your Computer With 8 Seconds o...
Phishing for Root (How I Got Access to Root on Your Computer With 8 Seconds o...
 
Bash Scripting Workshop
Bash Scripting WorkshopBash Scripting Workshop
Bash Scripting Workshop
 

Recently uploaded

GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
kumardaparthi1024
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
SOFTTECHHUB
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
Zilliz
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
Daiki Mogmet Ito
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
Mariano Tinti
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Speck&Tech
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
IndexBug
 

Recently uploaded (20)

GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
 

Shell实现的windows回收站功能的脚本

  • 1. : << EOF trash 命令替代linux rm命令实现windows回收站的功能 一. 功能简介 1. 将删除的文件放在回收站中 2. 恢复删除的文件 3. 实现linux rm命令的功能, 使用起来几乎和linux 系统自带的rm ,命令完全一样 4. 新增功能: rm -l, rm -e, rm -c 5. 该脚本每次在运行时候会检查$HOME/.trash 目录下文件大小之和, 若 超过最大容量, 脚本会自动将日志文件中所记录文件中的前一半文件从回 收站中清除,所以建议删除大文件(相对于回收站最大容量而言)直接用 命令/bin/rm 而不要用 rm. 二. 使用方法: 1. 将trash文件放到 $HOME/bin/ 2. 在$HOME/.bashrc 文件中加入alias rm=”$HOME/bin/trash”, 重新登陆终端或执行bash命令。 3. 执行命令rm -e 配置回收站的最大容量,单位K 4. 回收站的默认目录为:$HOME/.trash, 默认配置文件为:$HOME/.trash/trash.conf 默认log文件为:$HOME/.trash/trash.log 5. 怎样恢复文件: 在linux 终端中输入rm -l, 然后 在RowNumber: 后面键入要删除文件所在的行标识:988 键入y/Y 然后按回车键 恢复成功. 如果想只查看删除列表, 则键入rm -l 后直接按回车键或者键入Q/q 6. 更详细的参数介绍请键入:rm --help 三. 注意事项 1. 想要手动清空$HOME/.trash目录需要用/bin/rm命令, 请不要尝试用rm -r $HOME/.trash 的方法. 2. 该脚本不支持rm -r -f , rm -rfi (选项组合超过2个)格式. 3. 如果你可以你甚至可以用该脚本作为备份脚本, 假若想备份test2.txt你只需要执行rm test2.txt, 当然如果真想备份某个文件的话, 最好编写专门的备份脚本。 EOF =======================CODE======================== #!/bin/bash #配置回收站最大的存储空间(字节) #maxmemory=51200 (50M) #maxmemory=102400 (100M) #maxmemory=512000 (500M) #根据情况设置为50M(对于isoa服务开发来说足够了) maxmemory=204800 #设置回收站所在的目录 trash=$HOME/.trash #设置日志文件所在的目录 mvlog=$trash/trash.log from1=$1 from2=$2 var_pwd= var_father= #回收站若不存在,则新建之 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++)) do echo -n ${a[$RANDOM%${#a[*]}]} done } random=$(rand) #文件不存在时的提示信息 function file_null() { local file=$1 echo "rm: cannot remove '$file': No such file or directory" file:///home/aaron/nfs/20130816/trash.sh.html 1 of 6 08/16/2013 01:25 PM
  • 2. } #打印参数出错后的提示信息 function echo_msg() { echo -n "rm: missing operand Try 'rm --help' for more information. " } function echo_msg2() { echo -n "rm: invalid option '$1' Try 'rm --help' for 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"'/'"$2"':'"$3""); #else print $0}' $mvlog | sort -o $mvlog awk -F: -v nu=$num -v trash=$trash '{if (NR<=nu) system("rm -rf "trash"'/'"$2"':'"$3""); else print $0}' $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 $from1) local dirname=$(dirname $from1) jug_cur $dirname if [ "$JUG" -eq 0 ];then dirname=$var_pwd elif [ $JUG -eq 1 ];then dirname=$var_father fi if [ -d "$from1" ];then echo "rm: cannot remove '$from1': Is a directory" else if [ ! -e $from1 ];then file_null $from1 else echo "$dirname:$filename:$random:$(date +%Y-%m-%d.%T)" >> $mvlog mv "$from1" "$trash/$filename:$random" fi fi } #rm -i function rmi() file:///home/aaron/nfs/20130816/trash.sh.html 2 of 6 08/16/2013 01:25 PM
  • 3. { local filename=$(basename $from2) local dirname=$(dirname $from2) jug_cur $dirname if [ $JUG -eq 0 ];then dirname=$var_pwd elif [ $JUG -eq 1 ];then dirname=$var_father fi if [ -f "$from2" ];then echo -n "rm: remove regular file '$from2'?" read answer if [ "$answer" = 'y' -o "$answer" = 'Y' ];then echo "$dirname:$filename:$random:$(date +%Y-%m-%d.%T)" >> $mvlog mv "$from2" "$trash/$filename:$random" fi else if [ ! -e $from2 ];then file_null $from2 else echo "rm: cannot remove '$from2': Is a directory" fi fi } #rm -f function rmf() { local filename=$(basename $from2) local dirname=$(dirname $from2) jug_cur $dirname if [ $JUG -eq 0 ];then dirname=$var_pwd elif [ $JUG -eq 1 ];then dirname=$var_father fi if [ -f "$from2" ];then echo "$dirname:$filename:$random:$(date +%Y-%m-%d.%T)" >> $mvlog mv "$from2" "$trash/$filename:$random" else if [ ! -e $from2 ];then : else echo "rm: cannot remove '$from2': Is a directory" fi fi } #rm -r function rmr() { local filename=$(basename $from2) local dirname=$(dirname $from2) jug_cur $dirname if [ $JUG -eq 0 ];then dirname=$var_pwd elif [ $JUG -eq 1 ];then dirname=$var_father fi if [ "$from2" = "." -o "$from2" = ".." ];then echo "rm: cannot remove directory: '$from2'" elif [ -e "$from2" ];then echo "$dirname:$filename:$random:$(date +%Y-%m-%d.%T)" >> $mvlog mv "$from2" "$trash/$filename:$random" else file_null $from2 fi } #rm -rf function rmrf() file:///home/aaron/nfs/20130816/trash.sh.html 3 of 6 08/16/2013 01:25 PM
  • 4. { local filename=$(basename $from2) local dirname=$(dirname $from2) jug_cur $dirname if [ $JUG -eq 0 ];then dirname=$var_pwd elif [ $JUG -eq 1 ];then dirname=$var_father fi if [ "$from2" = "." -o "$from2" = ".." ];then echo "rm: cannot remove directory: '$from2'" elif [ -e "$from2" ];then echo "$dirname:$filename:$random:$(date +%Y-%m-%d.%T)" >> $mvlog mv "$from2" "$trash/$filename:$random" else : fi } #rm -ir function rmir() { local filename=$(basename $from2) local dirname=$(dirname $from2) jug_cur $dirname if [ $JUG -eq 0 ];then dirname=$var_pwd elif [ $JUG -eq 1 ];then dirname=$var_father fi if [ -e "$from2" ];then if [ -d "$from2" ];then echo -n "rm: remove directory '$from2'?" else echo -n "rm: remove regular file '$from2'?" fi read answer if [ "$answer" = 'y' -o "$answer" = 'Y' ];then echo "$dirname:$filename:$random:$(date +%Y-%m-%d.%T)" >> $mvlog mv "$from2" "$trash/$filename:$random" fi else if [ ! -e $from2 ];then file_null $from2 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 $1, "FileName:"$2, "Time: "$4":"$5":"$6}') linecount=$(cat $mvlog | wc -l) echo -e "$line" echo echo file:///home/aaron/nfs/20130816/trash.sh.html 4 of 6 08/16/2013 01:25 PM
  • 5. echo "[$linecount] Please enter the file you want to restore (replaced with the line number)" printf "RowNumber: " read answer if [ "$answer" = 'q' -o "$answer" = 'Q' -o "$answer" = "" ];then : else printf "Please confirm (Y/N): " read answer1 if [ "$answer1" = 'y' -o "$answer1" = 'Y' ];then address=$(sed -n "$answer""p" $mvlog | awk -F: '{print $1}') filename=$(sed -n "$answer""p" $mvlog | awk -F: '{print $2}') filerand=$(sed -n "$answer""p" $mvlog | awk -F: '{print $3}') 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 with 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 that if you use rm to remove a file, it is usually possible to recover the contents of that file. If you want more assurance that the contents are truly unrecoverable, consider using shred. Report bugs to <bug-coreutils@gnu.org>. EOF } #脚本开始 #检测回收站已用存储空间,如果已经达到最大值,则删除日志文件中位于前面的一半的文件 mem=$(du -s $trash|awk '{print $1}') if [ "$mem" -gt $maxmemory ];then deal fi if [ "$#" -eq 0 ];then echo_msg fi if [ "$#" -eq 1 ];then case "$from1" in -i) echo_msg ;; -f) file:///home/aaron/nfs/20130816/trash.sh.html 5 of 6 08/16/2013 01:25 PM
  • 6. 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 $from1 ;; *) rm1 ;; esac fi if [ "$#" -ge 2 ];then until [ "$2" = "" ] do from2=$2 case "$from1" 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 $from1 exit 1 ;; *) { until [ "$1" = "" ] do from1=$1 rm1 shift done } ;; esac shift done fi exit file:///home/aaron/nfs/20130816/trash.sh.html 6 of 6 08/16/2013 01:25 PM