linux中防止误删文件的方法

发布时间 2023-10-16 14:52:01作者: e-8bit

安装trash-cli

# 更换国内源
rambo@debian:~$ cat /etc/apt/sources.list
deb https://mirrors.aliyun.com/debian/ bookworm main non-free non-free-firmware contrib
deb-src https://mirrors.aliyun.com/debian/ bookworm main non-free non-free-firmware contrib
deb https://mirrors.aliyun.com/debian-security/ bookworm-security main
deb-src https://mirrors.aliyun.com/debian-security/ bookworm-security main
deb https://mirrors.aliyun.com/debian/ bookworm-updates main non-free non-free-firmware contrib
deb-src https://mirrors.aliyun.com/debian/ bookworm-updates main non-free non-free-firmware contrib
deb https://mirrors.aliyun.com/debian/ bookworm-backports main non-free non-free-firmware contrib
deb-src https://mirrors.aliyun.com/debian/ bookworm-backports main non-free non-free-firmware contrib

deb https://mirrors.ustc.edu.cn/debian/ bookworm main non-free non-free-firmware contrib
deb-src https://mirrors.ustc.edu.cn/debian/ bookworm main non-free non-free-firmware contrib
deb https://mirrors.ustc.edu.cn/debian-security/ bookworm-security main
deb-src https://mirrors.ustc.edu.cn/debian-security/ bookworm-security main
deb https://mirrors.ustc.edu.cn/debian/ bookworm-updates main non-free non-free-firmware contrib
deb-src https://mirrors.ustc.edu.cn/debian/ bookworm-updates main non-free non-free-firmware contrib
deb https://mirrors.ustc.edu.cn/debian/ bookworm-backports main non-free non-free-firmware contrib
deb-src https://mirrors.ustc.edu.cn/debian/ bookworm-backports main non-free non-free-firmware contrib



# 创建示例文件
rambo@debian:~$ echo 123 > 1.txt
rambo@debian:~$ mkdir opop && echo 456 > opop/2.txt


# 安装trash-cli
rambo@debian:~$ sudo apt-get install trash-cli
# 查看回收站时为空
rambo@debian:~$ trash-list

# 不建议超级用户使用这样的别名
rambo@debian:~$ vim ~/.bashrc
alias rm=trash-put

rambo@debian:~$ source .bashrc


# 将1.txt文件和opop目录移动到回收站中
rambo@debian:~$ trash-put 1.txt opop   或者  rm -rf 1.txt opop(在做了别名的前提下可这样操作)

# 查看回收站中已删除的文件
rambo@debian:~$ trash-list 
2023-10-16 02:24:55 /home/rambo/1.txt
2023-10-16 02:24:55 /home/rambo/opop

# 从回收站中恢复文件或目录
rambo@debian:~$ trash-restore
   0 2023-10-16 02:24:55 /home/rambo/1.txt
   1 2023-10-16 02:24:55 /home/rambo/opop
What file to restore [0..1]: 0    # 询问你想恢复的文件或目录,这里我选择0,即1.txt,只能使用该方式恢复

rambo@debian:~$ cat 1.txt 
123


# 从回收站中删除文件
rambo@debian:~$ trash-rm opop
rambo@debian:~$ trash-list              # 再次查看回收站时将为空


# 删除超过 N 天的垃圾文件:或者可删除回收站中超过 N 天的文件
rambo@debian:~$ trash-empty 30          # 删除回收站中超过 10 天的文件

autotrash

rambo@debian:~$ sudo apt-get install -y pipx
rambo@debian:~$ pipx install autotrash
# 添加到环境变量中
rambo@debian:~$ pipx ensurepath


往后的步骤需要自己来完成....

/etc几乎包含所有配置文件,包括库的配置文件、系统的主机名和日志记录以及访问和 sudo 信息以及系统的 shell 初始化文件以及用户和组数据库,即便使用Live USB/CD,它不会有您在安装系统本身后安装的软件包或您更改的任何内容的任何配置文件