Skip to content

Instantly share code, notes, and snippets.

@ChenYFan
Last active July 5, 2024 08:19
Show Gist options
  • Save ChenYFan/9f94def100455f16413e6db53616e834 to your computer and use it in GitHub Desktop.
Save ChenYFan/9f94def100455f16413e6db53616e834 to your computer and use it in GitHub Desktop.
TrashFile
#回收站目录
TrashRoot=~/TrashFile
TrashData=$TrashRoot/Trash.data
TrashDir=$TrashRoot/TrashDir
alias RealRm='/bin/rm'
#创建回收站目录
if [ ! -d $TrashRoot ]; then
mkdir $TrashRoot
mkdir $TrashDir
touch $TrashData
fi
realDeleteFile(){
local args=($@)
echo "You are trying to delete file(s) WITHOUT TrashFile:"${args[@]}
echo "It will make the file(s) unrecoverable,and TrashFile will not be able to help you to restore it."
read -p "Are you sure to delete this file? [y/n]" choice
if [ $choice != "y" ]; then
echo "Abort"
return
fi
read -p "Please Check again, Are you sure to delete this file? [y/n]" choice
if [ $choice != "y" ]; then
echo "Abort"
return
fi
read -p "Please Check again, Are you sure to donot delete this file? [y/n]" choice
if [ $choice != "n" ]; then
echo "Abort"
return
fi
echo "Sure,Wait for 3 seconds"
sleep 3
RealRm $@
}
seeTrash(){
cat $TrashData
}
#删除文件
deleteFile(){
#获取文件名。不识别参数,只识别文件名
local args=($@)
local filePath=${args[${#args[@]}-1]}
#如果存在不止一个文件,则批量删除
if [ ${#args[@]} -gt 1 ]; then
echo "You are deleting multiple files:"${args[@]}
echo "Are you sure to delete these files? [y/n]"
read choice
if [ $choice != "y" ]; then
echo "Abort"
return
fi
for file in ${args[@]}
do
deleteSingleFile $file
done
elif [ ${#args[@]} -eq 1 ]; then
deleteSingleFile $filePath
fi
}
restoreFile(){
#获取文件名。不识别参数,只识别文件名
local args=($@)
local filePath=${args[${#args[@]}-1]}
#如果存在不止一个文件,则批量删除
if [ ${#args[@]} -gt 1 ]; then
echo "You are restoring multiple files:"${args[@]}
echo "Are you sure to restore these files? [y/n]"
read choice
if [ $choice != "y" ]; then
echo "Abort"
return
fi
for file in ${args[@]}
do
restoreSingleFile $file
done
elif [ ${#args[@]} -eq 1 ]; then
restoreSingleFile $filePath
fi
}
deleteSingleFile(){
#获取文件名。不识别参数,只识别文件名
local args=($@)
local filePath=${args[${#args[@]}-1]}
#获取文件路径
local fileName=$(basename $filePath)
#绝对路径
local fileDir=$(cd "$(dirname "$filePath")";pwd)
#不存在则返回
if [ ! -e $filePath ]; then
echo "No such file"
return
fi
#获取删除时间
local deleteTime=$(date "+%Y-%m-%d-%H-%M-%S")
#移动文件
local TrashFileName="."$deleteTime"_"$fileName
local TrashFilePath=$fileDir"/"$TrashFileName
mv $filePath $TrashFilePath".rm"
#创建软链接
ln -s $TrashFilePath".rm" $TrashDir"/"$TrashFileName
echo $TrashFilePath".rm" >> $TrashData
}
restoreSingleFile(){
local fileName=$(basename $filePath)
#如果源文件已存在,则提示用户是否覆盖
if [ -e $fileName ]; then
read -p "File already exists, do you want to overwrite it? [y/n]" choice
if [ $choice != "y" ]; then
return
fi
read -p "Please Check again, do you want to OVERWRITE it? [y/n]" choice
if [ $choice != "y" ]; then
return
fi
read -p "Do you wanna to NOT backup the file? [y/n]" choice
if [ $choice != "y" ]; then
deleteFile $filePath
fi
fi
local grepFiles=$(ls -a | grep "_"$fileName".rm")
local fileNum=$(echo $grepFiles | wc -w)
if [ $fileNum -eq 0 ]; then
echo "No such file in TrashDir"
return
elif [ $fileNum -eq 1 ]; then
local TrashFileName=$(echo $grepFiles | awk '{print $1}')
else
echo "More than one file found, please choose one:"
local i=1
for file in $grepFiles
do
echo $i": "$file
i=$(($i+1))
done
read -p "Please input the number of the file you want to restore:" num
local TrashFileName=$(echo $grepFiles | awk '{print $'$num'}')
fi
local RealFileName=$(echo $TrashFileName | sed 's/^\..*_//' | sed 's/\.rm//')
echo "Restoring "$TrashFileName" to "$RealFileName
mv $TrashFileName $RealFileName
local TrashFileNameWithNoSuffix=$(echo $TrashFileName | sed 's/\.rm//')
#删除Trash.data中的记录
sed -i '/'$TrashFileNameWithNoSuffix'/d' $TrashData
#删除软链接
RealRm -rf $TrashDir"/"$TrashFileNameWithNoSuffix
}
clearTrashWithTimeLimit(){
local NowTime=$(date "+%s")
local timeLimit=$1
if [ -z $timeLimit ]; then
timeLimit=$(expr 60 \* 60 \* 24 \* 15)
fi
echo "Clearing TrashFile with time limit "$timeLimit" seconds"
cat $TrashData | while read line
do
local trashedFilePath=$(echo $line | awk '{print $1}')
local trashedFileName=$(basename $trashedFilePath)
local trashedFileTime=$(echo $trashedFileName | sed 's/^\.\([0-9]*-[0-9]*-[0-9]*-[0-9]*-[0-9]*-[0-9]*\)_.*/\1/' | sed 's/-/ /3' | sed 's/-/:/3' | sed 's/-/:/3')
local trashedFileTime=$(date -d "$trashedFileTime" "+%s")
local timeDiff=$(expr $NowTime - $trashedFileTime)
if [ $timeDiff -gt $timeLimit ]; then
echo "Deleting "$trashedFilePath
RealRm -rf $trashedFilePath
sed -i '/'$trashedFileName'/d' $TrashData
RealRm -rf $TrashDir"/"$trashedFileName
fi
done
echo "Clearing Done"
}
clearTrash(){
read -p "Are you sure to clear TrashFile? [y/n]" choice
if [ $choice != "y" ]; then
echo "Abort"
return
fi
clearTrashWithTimeLimit 0
> $TrashData
RealRm -rf $TrashDir
mkdir $TrashDir
}
#覆写rm
alias rrm='realDeleteFile'
alias rm='deleteFile'
alias rf='restoreFile'
alias ct='clearTrash'
alias ctl='clearTrashWithTimeLimit'
alias st='seeTrash'
@Guogb
Copy link

Guogb commented Jun 6, 2024

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment