为Centos7创建回收站,防止误删文件

由于Linux不像Windows一样,有专门的回收站,因此如果文件误删了,就很难找回来了。这里提供了一种为Centos7创建回收站的方法,可以有效地防止误删文件,并对删除信息进行记录。

尝试与修改

在准备实现这个功能的时候,最开始就是想去网上找案例的,但是找来找去,网上的案例都不是很让我满意。可能至少有以下问题中的一个或多个问题:

  • 需要使用者记住某一个命令,使用专门的命令才能移动到回收站,直接使用rm命令就直接删除了。
  • 只能root用户删除文件时才是移动到回收站,其他用户删除文件时就直接删除了。
  • root账户和其他用户都可以将删除的文件移动到回收站,但是用户如果使用sudo命令进行删除操作时,会直接把文件删除而不是移动到回收站。
  • 所有用户共享一个回收站,导致一个用户可以查看到其他用户删除的文件,暴露隐私。
  • 所有删除的文件没有分类,不便于查阅。
  • 删除的文件没有记录,无法知道移动到回收站的文件是从哪移动过来的。

针对以上问题,我这边现学了一下Shell脚本的编写,然后现学现卖,对网上的案例进行了一次大改,一路解决了不少问题,终于实现了自己想要的功能。新的脚本具有以下特性:

  • 每个用户都可以使用回收站功能
  • 每个用户具有独立的回收站,用户删除的文件会移动到自己专属的回收站中,不会被未授权的用户看到。
  • 回收站内按照天建立文件夹,移入的文件添加时间后缀进行重命名,防止同名文件覆盖。
  • 可以记录删除记录,对每个文件的删除时间,源位置,删除到回收站中的位置进行记录。
  • 可以手动快速删除30天前移入回收站的文件,快速释放磁盘空间。
  • 直接使用rm命令,对使用者无感,即使是其他人来使用这个系统也可以使用回收站功能。

一起来学习一下吧。

操作流程

先在/usr/bin目录创建一个新文件,名字为delete,并填写如下内容:

#!/bin/bash

#########################################
# File Name: delete
# Date: 2023-04-08
# Version: v1.0
# Author: UUSITE
#########################################

# Records information. Such as when it was "deleted", original location, and location in the recycle bin
function log_trash() {
    file=$1
    mark1="."
    mark2="/"
    if [ "$file" = ${file/$mark2/} ]; then
        fullpath="$(pwd)/$file"
    elif [ "$file" != ${file/$mark1/} ]; then
        fullpath="$(pwd)${file/$mark1/}"
    else
        fullpath="$file"
    fi
    # The output format is: ${delete time} \t ${original location} \t ${location in the recycle bin}
    echo -e "$3\t$fullpath\t$2" >>$HOME/.trash/.log
}

# The function that actually performs the "delete" operation
function move_to_trash() {
    if [ ! -d $HOME/.trash/ ]; then
        mkdir -m 777 -p $HOME/.trash
        touch $HOME/.trash/.log
        chmod 666 $HOME/.trash/.log
    fi

    prefix=$(date +%Y_%m_%d)
    if [ ! -d $HOME/.trash/$prefix ]; then
        mkdir -p $HOME/.trash/$prefix
    fi
    files=()
    for arg in "$@"; do
        # If the input parameter is indeed a file, directory, or link, add it to the array
        if [[ -e "$arg" || -e "$arg" || -L "$arg" ]]; then
            files+=("$arg")
        fi
    done
    echo "move files to trash"
    for file in ${files[@]}; do
        if [ -f "$file" -o -d "$file" ]; then
            now=$(date +%Y%m%d_%H%M%S_%N)
            file=${file%/}
            filename=${file##*/}
            move_trash_path="${HOME}/.trash/${prefix}/${filename}_${now}"
            /usr/bin/mv $file $move_trash_path
            [ $? -eq 0 ] && log_trash $file $move_trash_path $now
        fi
    done
}

# If the number of parameters is 0, display help information
if [ $# -eq 0 ]; then
    echo "Usage: rm file1 [file2 file3....]"
    exit 128
fi
move_to_trash "$@"

继续在/usr/bin目录创建一个新文件,名字为cleantrash,并填写如下内容:

#!/bin/bash

#########################################
# File Name: cleantrash
# Date: 2023-04-08
# Version: v1.0
# Author: UUSITE
#########################################

# Clean up files that were moved to the recycle bin 30 days ago
now=$(date +%s)
for s in $(ls --indicator-style=none $HOME/.trash/); do
    dir_name=${s//_/-}
    dir_time=$(date +%s -d $dir_name)
    # If the file is moved to the recycle bin for more than one month, delete it
    if [[ 0 -eq dir_time || $(($now - $dir_time)) -gt 2592000 ]]; then
        /bin/rm -rf $s
    fi
done
echo "trash files has gone"

修改/etc/bashrc文件,添加如下内容:

alias sudo='sudo '
alias rm='delete'

执行source /etc/bashrc文件,就可以开始使用了!

使用方法

操作简述

之后我们可以使用rm命令进行删除文件,删除的文件会自动移动到回收站中。每个用户都拥有自己的回收站,回收站位于$HOME/.trash,移动到回收站的文件会根据日期按文件夹分类,并且每个删除记录都会写入回收站中的.log文件中。如果需要删除30天前移入回收站中的文件,可以使用cleantrash命令删除。所有相关的文件夹和文件都无需自己新建,如果检测到不存在,会自动创建好。相对来说较人性化。

如果想直接将文件删除,而不是移动到回收站,可以使用/bin/rm进行删除。

操作演示

查看当前用户的回收站位置$HOME/.trash指向的具体位置:

[index@localhost test]$ echo $HOME/.trash
/home/index/.trash

查看当前目录下所有的文件:

[index@localhost test]$ ls -lrht
total 0
-rw-rw-r--. 1 index index 0 Apr  9 10:50 1.txt
drwxrwxr-x. 2 index index 6 Apr  9 10:50 new

删除当前目录下所有的文件:

[index@localhost test]$ rm -rf ./*
move files to trash

进入当前用户的回收站,并查看回收站中的文件:

[index@localhost test]$ cd /home/index/.trash/
[index@localhost .trash]$ ls -alrht
total 4.0K
drwx------. 4 index index 109 Apr  9 10:50 ..
drwxrwxrwx. 3 index index  36 Apr  9 10:56 .
drwxrwxr-x. 3 index index  82 Apr  9 10:56 2023_04_09
-rw-rw-rw-. 1 index index 657 Apr  9 10:56 .log

查看.log文件中的删除记录:

[index@localhost .trash]$ cat .log 
20230409_105634_390533589       /home/index/test/1.txt  /home/index/.trash/2023_04_09/1.txt_20230409_105634_390533589
20230409_105634_391991233       /home/index/test/new    /home/index/.trash/2023_04_09/new_20230409_105634_391991233

.log文件一共有3列,第一列是删除时间,第二列是文件删除前的位置,第三列是文件位于回收站中的位置

进入具体的日期文件夹,查看删除的文件:

[index@localhost .trash]$ cd 2023_04_09/
[index@localhost 2023_04_09]$ ls -lrht
total 0
-rw-rw-r--. 1 index index 0 Apr  9 10:50 1.txt_20230409_105634_390533589
drwxrwxr-x. 2 index index 6 Apr  9 10:50 new_20230409_105634_391991233

可以看到删除的文件自动被重命名了,可以防止重名文件相互覆盖。

参考资料

原创文章,创作不易,版权所有,抄袭必究

作者:幽叶草。如若转载,需要先经过作者同意,并附上原文链接 https://blog.uusite.com/system/linux/408.html

(1)
幽叶草的头像幽叶草
上一篇 2023年3月31日 下午4:16
下一篇 2023年5月8日 下午11:57

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注