在LINUX上实现 windows 回收站功能的脚本

该博客介绍了一个自定义rm命令脚本的功能,执行rm命令时会将文件移至/dump-file/目录,按原文件名加删除日期命名,超2GB则直接删除。还给出了安装需求,包括创建存放目录、日志文件,移动系统rm命令,命名脚本并赋予权限等。

脚本功能:

  1. 在用户执行rm命令时,将文件移动/dump-file/ ,被 移动到dump-file 目录下的文件、文件夹以原文件名+删除除的日期格式存放,如 weather.txt 在删除后就会被自动移动到/dump-file/ 下并命名为weather.txt_2010-05-07_14:46:51.bak
  2. 此命令在执行时会检测文件或目录的大小,如果文件或目录超过2GB,将不会再将文件移动到回收站,而会直接删除,此功能是为了避免回收站过大而对系统造成影响

安装需求:
1.创建删除文件存放目录mkdir /dump-file/ && chmod -R 777 /dump-file
2.创建日志输出文件 touch /tmp/rm.dump && chmod -R 777 /tmp/rm.dump
3.将系统rm命令移动成 rm.bak : mv /bin/rm /bin/rm.bak
4.将此脚本命名为/bin/rm 并给予可执行权限 :chmod 644 /bin/rm

#!/bin/bash
DumpFile=/dump-file/
File=echo $line|awk '{print $2}'
aa=$(echo $1|grep “^-”)

function TestDir(){

Test_Dir=$(echo KaTeX parse error: Expected 'EOF', got '#' at position 16: line|grep "/") #̲test if is a di…(echo $line |awk '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲'|grep "^/") # …(echo $line |awk -F/ '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲') #Extract the…(echo $line |awk ‘{print $2}’|awk -F/ ‘{print $1}’) ##Extract the prameter before the first “/”.
D1=date +%F_%H:%M:%S.bak
Date=echo $line | awk '{print $2 "_" D1}' D1=$D1

    if [[ $Test_Dir != "" ]] ;then #If parameters include directory
            if [[ $Test_Dir2 != "" ]] ;then #If parameter starts with "/",as formate /a/b/c/
                    echo $line "start with /"
                    mkdir $DumpFile$Dir2"_"$D1 2>/tmp/rm.dump # Create directory as format /dump-file/(first directory after "/")
                   
                    mv `echo $line|awk '{print $2}'` $DumpFile$Dir2"_"$D1
                    echo $DumpFile$Dir2"_"$D1
            else #Means that parameter starts without "/" ,as format a/b/c
                    mkdir $DumpFile$Dir$D1 #Create directory as format /dump-file/(first directory before "/")
                    mv `echo $line|awk '{print $2}'` $DumpFile$Dir$D1
                    echo $line "starts before /"
            fi
    else
            Date=`echo $line | awk '{print $2 "_" D1}' D1=$D1`
            mv `echo $line|awk '{print $2}'` $DumpFile$Date
    fi

}

if [[ “$1” != “” ]] ;then #If the frist parameter is not empty.

    if [[ $aa != "$1" ]];then #If the first parameter not start with "-".
            du -s "$@" 2>/tmp/rm.dump |while read line
            do
                    #TestDir
                    size=`echo $line | awk '$1 >2000000{print $1}'`
                    if [ "$size" > "1900000" ];then
                            echo "Lager than 2Mb"
                    else
                            TestDir
                    fi
            done

    else
            if [[ "$2" != ""  ]]; then
                   until [ "$2" == ""  ]
                   do
                             #echo "function is running"
                             du -s $2 2> /tmp/rm.dump |while read line
                             do
                                    size=`echo $line | awk '$1 >2000000{print $1}'`
                                    if [ "$size" > "1900000" ];then
                                            echo "Lager than 2Mb"
                                            rm.bak -rf $2
                                    else
                                            TestDir

                                    fi
                                    shift
                             done
                             shift
                    done
            else
                    echo -e "No file detected.\nTry 'rm --help' for more information. "
            fi
    fi

else
echo -e “rm: missing operand \nTry ‘rm --help’ for more information.”
fi

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值