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

删除是危险系数很高的操作,一旦误删可能会造成难以估计的损失。比如,一条简单的语句:rm –rf /* 就会把整个系统全部删除,而 Linux 并不会因为这条语句的不合理而拒绝执行。

在 Windows 中,为了防止误删,系统默认提供了回收站功能。用户在执行删除操作后,文件并不会直接从硬盘中删除,而是被放到回收站中。在清空回收站前,如果发现有文件被误删,用户可以将回收站中的文件恢复到原来的位置。而 Linux 并没有提供类似功能,删除命令 rm 一旦确认执行,文件就会直接从系统中删除,很难恢复。

本文以 CentOS 系统环境为例,给 Linux 创建一个简单的回收站功能。

回收站脚本具有以下特性:

● 每个用户都可以使用回收站功能

● 每个用户具有独立的回收站,用户删除的文件会移动到自己专属的回收站中,不会被未授权的用户看到。

● 回收站内按照天建立文件夹,移入的文件添加时间后缀进行重命名,防止同名文件覆盖。

● 可以记录删除记录,对每个文件的删除时间,源位置,删除到回收站中的位置进行记录。

● 可以手动快速删除30天前移入回收站的文件,快速释放磁盘空间。

● 直接使用rm命令,对使用者无感,即使是其他人来使用这个系统也可以使用回收站功能。

操作流程

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

#!/bin/bash

#########################################
# File Name: delete
# Date: 2024-08-15
# Version: v1.0
# Author: double
#########################################

# 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: 2024-08-15
# Version: v1.0
# Author: double
#########################################

# 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'

也可使用sed命令修改

sed -i '/^alias sudo=/d' /etc/bashrc
sed -i '/^alias rm=/d' /etc/bashrc
sed -i '$a\alias sudo='\''sudo '\''\nalias rm='\''delete'\''' /etc/bashrc
执行source /etc/bashrc文件,就可以开始使用了!

测试rm删除是否进入回收站

cd ~
echo "This is a test file." > test_file.txt
rm -rf test_file.txt
cd ~/.trash/

使用方法

操作简述

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

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

操作演示

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

[root@test ~]# echo $HOME/.trash
/root/.trash

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

[root@test ~]# ll
总用量 13240
-rw-------. 1 root root     1245 124 2024 anaconda-ks.cfg
-rw-r--r--. 1 root root 13550032 617 05:45 kernel-lt-devel-5.4.278-1.el7.elrepo.x86_64.rpm

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

[root@test ~]# rm -rf ./*
move files to trash

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

[root@test .trash]# ll
总用量 4
drwxr-xr-x. 2 root root 4096 814 20:37 2024_08_14

查看回收站日志.log文件中的删除记录:

[root@test .trash]# cat .log
20240814_203754_935761544       /root/anaconda-ks.cfg   /root/.trash/2024_08_14/anaconda-ks.cfg_20240814_203754_935761544
20240814_203754_943863608       /root/kernel-lt-devel-5.4.278-1.el7.elrepo.x86_64.rpm   /root/.trash/2024_08_14/kernel-lt-devel-5.4.278-1.el7.elrepo.x86_64.rpm_20240814_203754_943863608

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

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

[root@test .trash]# cd 2024_08_14
[root@test 2024_08_14]# ll
总用量 64972
-rw-------. 1 root root     1245 124 2024 anaconda-ks.cfg_20240814_203754_935761544
-rw-r--r--. 1 root root 13550032 617 05:45 kernel-lt-devel-5.4.278-1.el7.elrepo.x86_64.rpm_20240814_203754_943863608

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

如果想彻底删除,则使用系统原生rm命令删除即可

/bin/rm -rf test.log

服务器批量配置回收站

先将delete、cleantrash脚本放到nginx服务器的下载目录

cd /usr/bin

#下载可执行文件
wget -O delete http://192.168.1.18:8080/delete
wget -O cleantrash http://192.168.1.18:8080/cleantrash

#授权
chmod +x delete cleantrash

#设置指令的别名
sed -i '/^alias sudo=/d' /etc/bashrc
sed -i '/^alias rm=/d' /etc/bashrc
sed -i '$a\alias sudo='\''sudo '\''\nalias rm='\''delete'\''' /etc/bashrc

source /etc/bashrc

nginx下载配置

server {
    listen 8080;
    server_name 192.168.1.18;

    # 根目录设置
    root /home/app/web/download;

    # 启用目录列表
    location / {
        autoindex on; # 启用目录列表
        autoindex_exact_size off; # 以更人性化的格式显示文件大小
        autoindex_localtime on; # 以服务器本地时间显示文件时间
        charset utf-8; # 设置字符集为 UTF-8
        try_files $uri $uri/ =404; # 确保请求的文件或目录存在,否则返回 404
    }

    # 禁止访问上级目录,防止目录遍历攻击
    location ~ /\.\./ {
        deny all;
    }

    # 禁止访问 .ht* 文件
    location ~ /\.ht {
        deny all;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值