首页 > 系统相关 >Linux创建回收站,防止误删文件 误删除文件恢复

Linux创建回收站,防止误删文件 误删除文件恢复

时间:2024-06-18 10:59:36浏览次数:14  
标签:误删除 文件 file bin HOME trash 回收站

使用为Centos7创建回收站的方法,可以有效地防止误删文件,并对删除信息进行记录。

实现:

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

步骤:

1、/usr/bin目录创建两个新文件,名字为delete、cleantrash,并赋予可执行权限

#!/bin/bash

#########################################
# File Name: delete
# Date: 2024-06-18
# Version: v1.0
# Author: jason
#########################################

# 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 "$@"
#!/bin/bash

#########################################
# File Name: cleantrash
# Date: 2024-06-18
# Version: v1.0
# Author: jason
#########################################

# Clean up files that were moved to the recycle bin 3 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 259200 ]]; then
        /bin/rm -rf $s
    fi
done
echo "trash files has gone"

chmod +x delete  cleantrash

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

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

source /etc/bashrc

3、cleantrash加入定时任务

0 0 3 * *  /usr/bin/cleantrash

 

测试:

1、查看当前账户的trash目录

[root@localhost bin]# echo $HOME/.trash
/root/.trash

2、测试删除文件和目录

[root@localhost home]# rm -rf test.txt 
move files to trash

[root@localhost jason]# rm -rf dnsub_linux_amd64_v2.0/
move files to trash

3、查看删除的文件和删除日志记录

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

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

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

 

 

 

参考:

https://blog.uusite.com/system/linux/408.html

 

标签:误删除,文件,file,bin,HOME,trash,回收站
From: https://www.cnblogs.com/xiaoyou2018/p/18253874

相关文章

  • Docker+Jenkins+Pipline实现Vue项目input选择不同差异性config文件并修改文件内容后打
    场景Docker+Jenkins+Pipline实现SpringBoot项目input选择不同差异性yml文件打包、执行sh打包压缩包、使用archiveArtifacts下载制品(jar包、压缩包):https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/139748758DockerCompose+Jenkins+Pipeline流水线打包Vue项目(解压......
  • 多种总部文件下发方式比较,哪一种既相应业务效率又保证安全?
    大型企业在全国甚至全球都会设有分公司、办事处、生产工厂、研发中心等不同形式的分支机构,在日常经营中,存在多种总部文件下发的场景,如将公司战略规划与考核、规章制度、新产品信息及定价策略、业务培训指导材料、客户数据及资料、内部通知,红头文件、合同协议等。总部文件下发包......
  • Mac常用sh文件
    压缩多个文件夹到一个ZIP#!/bin/bash#定义目标目录target_dir="/Users/yuqiu/****/"output_dir="$target_dir/ZIPDIR"#获取当前日期current_date=$(date+"%Y-%m-%d")#定义压缩文件名zip_filename="XX_${current_date}.zip"#检查目标目录是否存在if[!-d......
  • HPC环境下文件流转最容易忽视的安全问题是什么?
    半导体芯片设计企业将IC设计、仿真、验证过程上云,已成为越来越广泛的共识。企业使用HPC环境能满足EDA工作负载前端仿真百万随机IO小文件,后端仿真海量顺序读写大文件的高并发访问需求,简化EDA的工作流程,降低了仿真作业的时间。  此外,HPC环境可以提供近乎无瓶颈的计算性......
  • 如何将keil5中的bin文件合并
    前言    最近有个需求,需要把单片机中的两个bin文件合并成一个bin文件,方便板子在生产烧录代码阶段可以节约烧录次数,这两个文件一般指的是BOOT+APP文件,bin文件里面没带有地址信息,但是在单片机中的烧录文件需要定位起始地址,所以就需要特别注意它们的偏移地址。因为可能会......
  • 【漏洞情报】泛微 E-Cology KtreeUploadAction 文件上传漏洞
    声明:请勿利用文章内的相关技术从事非法测试,由于传播、利用此文所提供的信息而造成的任何直接或者间接的后果及损失,均由使用者本人负责,作者不为此承担任何责任。如有侵权烦请告知,我们会立即删除并致歉。谢谢!01漏洞描述泛微OAE-CologyKtreeUploadAction存在文件上传漏洞......
  • (slam工具)1文件读取和保存
     1代码库https://github.com/Dongvdong/v1_1_slam_tool #-*-coding:utf-8-*-#condaactivatepy37gaosi#服务器#activatepy38#笔记本importosimportnumpyasnp'''#原始gnss输入四个数据名字纬度经度高度DJI_0002.JPG34.032505638888885......
  • 文件拆分脚本 && powershell移动文件脚本
    文件拆分脚本每隔两行拆分成一个新文件。importoswithopen('Main.java','r',encoding='UTF-8')asfile:file_content=file.read()file_parts=file_content.split('\n\n')foriinrange(len(file_parts)):fp=o......
  • 实验7 文件应用编程
    task4.c1#include<stdio.h>23intmain(){4intcount=0;5charch;6FILE*fp;78fp=fopen("data4.txt","r");9if(fp==NULL){10printf("failtoopenfile\n");11......
  • c++万能头文件
    一、问题出现c/C++使用首先就是要开头头文件的引用,没有写头文件的程序基本都不会成功运行得到想要的结果,因为每个程序基本都避免不了一定的输入与输出,而输入与输出却在头文件#include/#include<stdio.h>中大量的库函数扑面而来,随之产生了一个很令人头疼的问题,每一种类型的函......