linux下没有回收站机制,rm删除可能会造成不可逆转的文件丢失,一下是两个简单脚本命令实现回收站效果。
vim /bin/delete #创建删除命令
#!/bin/sh realrm="/bin/rm" if [ ! -d /tmp/trash ] then mkdir -v /tmp/trash chmod 777 /tmp/trash fiif [ $# -eq 0 ]
then
echo "Usage: delete file1 [file2 file3....]"
echo "If the options contain -f, then the script will exec 'rm' directly"
fiwhile getopts "dfiPRrvw" opt
do
case $opt in
f)
exec realrm "@"
;;
*)
# do nothing
;;
esac
doneecho -ne "Are you sure you want to move the files to the trash?[Y/N]:\a"
read reply
if [ reply = "y" -o reply = "Y" ]
then #####for file in $@
do
if [ -f "file" -o -d "file" ]
then
if [ -f "file" ] && [ `ls -l file|awk ' {print $5}'-gt 2147483648 ] then echo -ne "$(pwd)/$file size is larger than 2G, will be deleted directly, Are you sure you want to remove it?[Y/N]:\a" read choice until [ $choice == y -o $choice == n ]; do read -p "wrong choice, try again!" choice done if [ $choice == n ] then echo "the operation is abort!" else "removing the $file"
rm -rf $filefi elif [ -d "$file" ] && [
du -sb $file|awk '{print $1}'-gt 2147483648] then echo -ne "The directory:$(pwd)/$file is larger than 2G, will be deleted directly, Are you sure you want to remove it?[Y/N]:\a" read choice2 until [ $choice2 == y -o $choice2 == n ]; do read -p "wrong choice, try again!" choice done if [ $choice == n ] then echo "the operation is abort!" else "removing the $file"
rm -rf $filefi fi fi done fi now=
date +%Y%m%d_%H_%M_%S`
filename="${file##*/}"
newfilename="{file##*/}_{now}"
mark1="."
mark2="/"
if [ "file" = {file/$mark2} ]
then
fullpath="(pwd)/file"
elif [ "file" != {file/$mark1} ]
then
fullpath="(pwd){file/$mark1}"
else
fullpath="$file"
fiecho "the full path of this file is : $fullpath"
if mv -f file /tmp/trash/newfilename
then
(/bin/logTrashDir "newfilename filename now $fullpath")
echo "files: $file is deleted"
else
echo "the operation is failed"
fi
该脚本会创建/tmp/trash文件夹用于存储垃圾文件,大于2G的文件会提示是否直接删除,使用delete -f 可直接将文件删除而不用移到回收站。
以下两个脚本可以配合delete命令执行操作。
vim /bin/cleanTrashCan ###清理回收站命令
#!/bin/bash arrayA=($(find /tmp/trash/* -mtime +7 | awk '{print $1}')) for file in ${arrayA[@]} do $(rm -rf "${file}") filename="${file##*/}" echo $filename $(sed -i /$filename/'d' "/tmp/trash/.log") done
执行上面的脚本会删除回收站内7天前的文件和文件夹
vim /bin/LogTrashDir ###回收站日志脚本
#!/bin/sh if [ ! -f /tmp/trash/.log ] then touch /tmp/trash/.log chmod 700 /tmp/trash/.log fi echo $1 $2 $3 $4 >> /tmp/trash/.log
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于