[记录] Linux 回收脚本 recycle

回收 recycle

#!/bin/bash

# **********************************************************
# * Author          : 夜路沏茶
# * Technical       : 1530620364@qq.com
# * Filename        : recycle
# * Version         : 0.8.7
# * Description     : 回收文件或文件夹
# * -------------------------------------------------------
# * <>  必选参数。e.g. <-s> etc.
# * []  可选参数。e.g. [log] [-s] etc.
# * |   可选项。e.g. start|stop Y|S etc.
# * ... 重复项。e.g. file1, file2, ... etc.
# * -------------------------------------------------------
# * Last modified   : 2021-8-25 10:47:58
# * Last Update Log : 完善Tips提示信息;标识变更;
# * *******************************************************

folder="recycle_bin"
user="$(whoami)"

Tips() {
    echo "";
    echo "Usage: recycle <file1, file2, ...>"; echo "";
    echo "    e.g. recycle test.tar.gz";
    echo "         recycle *.log";
    echo "         recycle demo demo_bak";
    echo "";
    echo "Undo: undo-recycle [id]"; echo "";
    echo "    e.g. undo-recycle";
    echo "         undo-recycle $(date "+%Y%m%d%H%M%S%N")";
    echo "";
    echo "    recycle_bin($user) -> /home/$(whoami)/${folder}"; echo "";
    exit 1;
}

Moment() {
    nanoEnthalpyToMoment=$(date "+%N");
    momentTimeEnthalpyToMoment="";
    if [ -z "$1" ]; then momentTimeEnthalpyToMoment=$(date "+%Y-%m-%d_%T.%N")
    else momentTimeEnthalpyToMoment=$(date "+%Y-%m-%d_%H%M%S_")${nanoEnthalpyToMoment:0:$1}; fi
    echo "${momentTimeEnthalpyToMoment}"
}

LogError() {
    echo " - [ERROR] - $(Moment) - APP($0) - 「${id}」 - "$*
    if [ -z "${recycleLogPath}" ]; then
        echo " - [ERROR] - $(Moment) - APP($0) - 「${id}」 - "$* >> "/home/$(whoami)/logs/recycle.log";
    else
        echo " - [ERROR] - $(Moment) - APP($0) - 「${id}」 - "$* >> "${recycleLogPath}/recycle.log";
    fi; BlankLine;
}

LogInfo() {
    echo " - [INFO] - $(Moment) - APP($0) - 「${id}」 - "$*
    if [ -z "${recycleLogPath}" ]; then
        echo " - [INFO] - $(Moment) - APP($0) - 「${id}」 - "$* >> "/home/$(whoami)/logs/recycle.log";
    else
        echo " - [INFO] - $(Moment) - APP($0) - 「${id}」 - "$* >> "${recycleLogPath}/recycle.log";
    fi; BlankLine;
}

CreateDirectory() {
    if [ -z "${recycleLogPath}" ]; then
        mkdir -vp $* >> "/home/$(whoami)/logs/recycle.log";
    else
        mkdir -vp $* >> "${recycleLogPath}/recycle.log";
    fi; BlankLine;
}

BlankLine() {
    echo "";
    if [ -z "${recycleLogPath}" ]; then
        echo "" >> "/home/$(whoami)/logs/recycle.log"
    else
        echo "" >> "${recycleLogPath}/recycle.log";
    fi;
}

if [ -z "$1" ]; then Tips; exit 1; fi

id=$(date "+%Y%m%d%H%M%S%N");
momentFolder=/home/$user/$folder/$(Moment 3)

BlankLine
CreateDirectory "/home/$user/$folder" "/home/$user/logs" "$momentFolder"
LogInfo "Title: Recycle file @$user"
LogInfo "From: echo $(pwd)"
LogInfo "GoTo: $momentFolder"
mv "$@" "$momentFolder"
LogInfo "Command: mv $@ $momentFolder"
LogInfo "Recycle File: $@"
BlankLine

撤销 undo-recycle

#!/bin/bash

# * *******************************************************
# * Author          : 夜路沏茶
# * Technical       : 1530620364@qq.com
# * Filename        : undo-recycle
# * Version         : 0.5
# * Description     : 撤销回收
# * -------------------------------------------------------
# * Last modified   : 2021-8-27 11:48:09
# * Last Update Log : 修复撤销回收文件夹问题;
# * *******************************************************

# -------------------------- [ Get File ]

# shellcheck disable=SC2046
# shellcheck disable=SC2143
# shellcheck disable=SC2116
# shellcheck disable=SC2005
# shellcheck disable=SC2164
# shellcheck disable=SC2002
# shellcheck disable=SC2236

folder="recycle_bin"
user="$(whoami)"

getFileName() { if [ -z $(echo "$1" | grep -P -o ".+/") ]; then echo "$1"; else echo $(eval $(echo "echo \"$1\" | sed 's/$(echo "$1" | grep -P -o ".+/" | sed 's/\//\\\//g' | grep -P -o ".*")//'")); fi; }

getFilePath() { if [ -z $(echo "$1" | grep -P -o ".+/") ]; then echo "$CURRENT_PATH"; else echo "$1" | grep -P -o ".+/"; fi; }

# -------------------------- [ Get Content ]

getRecycleLog() { cat "/home/$(whoami)/logs/recycle.log" | grep "APP(/usr/sbin/recycle)"; }

getLastID() { getRecycleLog | grep -P -o "「[0-9]*」" | awk 'END {print}' | grep -P -o "[0-9]*"; }

getLastRecyclePath() { getRecycleLog | grep "$ID" | grep "GoTo" | awk '{print $NF}' | awk 'END {print}'; }

getLastPwd() { getRecycleLog | grep "$ID" | grep "From" | awk '{print $NF}' | awk 'END {print}'; }

getLastFileList() { getRecycleLog | grep "$ID" | awk 'END {print}' | grep -P -o ": .*" | sed 's/^: //'; }

Moment() {
    nanoEnthalpyToMoment=$(date "+%N");
    momentTimeEnthalpyToMoment="";
    if [ -z "$1" ]; then momentTimeEnthalpyToMoment=$(date "+%Y-%m-%d_%T.%N")
    else momentTimeEnthalpyToMoment=$(date "+%Y-%m-%d_%H%M%S_")${nanoEnthalpyToMoment:0:$1}; fi
    echo "${momentTimeEnthalpyToMoment}"
}

LogError() {
    echo " - [ERROR] - $(Moment 3) - APP($0) - 「$ID」 - "$*
    if [ -z "${recycleLogPath}" ]; then
        echo " - [ERROR] - $(Moment) - APP($0) - 「$ID」 - "$* >> "/home/$(whoami)/logs/recycle.log";
    else
        echo " - [ERROR] - $(Moment) - APP($0) - 「$ID」 - "$* >> "${recycleLogPath}/recycle.log";
    fi; BlankLine;
}

LogInfo() {
    if [ -z "${recycleLogPath}" ]; then echo " - [INFO] - $(Moment ) - APP($0) - 「$ID」 - "$* >> "/home/$(whoami)/logs/recycle.log";
    else echo " - [INFO] - $(Moment) - APP($0) - 「$ID」 - "$* >> "${recycleLogPath}/recycle.log"; fi;
}

BlankLine() {
    echo "";
    if [ -z "${recycleLogPath}" ]; then
        echo "" >> "/home/$(whoami)/logs/recycle.log"
    else
        echo "" >> "${recycleLogPath}/recycle.log";
    fi;
}

CreateDirectory() {
    if [ -z "${recycleLogPath}" ]; then mkdir -vp $* >> "/home/$(whoami)/logs/recycle.log";
    else mkdir -vp $* >> "${recycleLogPath}/recycle.log"; fi;
}


ID=$(getLastID)
CreateDirectory "/home/$user/$folder" "/home/$user/logs"

if [ ! -z "$1" ]; then
  TMP_IS_EXIST=$(getRecycleLog | grep "「$1」")
  if [ -z "${TMP_IS_EXIST[*]}" ]; then
    BlankLine; LogError "ID does not exist, search failed."
    exit 1;
  else
    ID=$1;
  fi
fi;

RECYCLE_PATH=$(getLastRecyclePath)
CURRENT_PATH=$(getLastPwd)
FILE_LIST=$(getLastFileList)

LogInfo "ID: $ID"
LogInfo "Recycle Path: $RECYCLE_PATH"
LogInfo "Recycle Pwd: $CURRENT_PATH"
LogInfo "File List: $FILE_LIST"

CreateDirectory "$CURRENT_PATH"
cd "$CURRENT_PATH"

for var in ${FILE_LIST[*]}; do
  itemInFileList=$(echo $var | sed 's/\/$//')
  LogInfo "Commands: id -> $ID, file -> $(getFileName "$itemInFileList"), path -> $(getFilePath "$itemInFileList"), mv \"$RECYCLE_PATH/$(getFileName "$itemInFileList")\" \"$(getFilePath "$itemInFileList")\""
  mv "$RECYCLE_PATH/$(getFileName "$itemInFileList")" "$(getFilePath "$itemInFileList")"
done

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值