Python trash-cli模块实现Linux服务器回收站

概述:

trash-cli是一个用于管理类 Unix 系统垃圾箱的命令行工具。它提供了一个安全的替代方案来代替传统的 `rm` 命令,后者会永久删除文件和目录。使用 trash-cli,文件和目录被移动到垃圾箱中,这样就可以在意外删除的情况下恢复它们。

以下是 trash-cli 的一些关键方面:
功能trash-cli 允许您从命令行执行各种与垃圾箱相关的操作,如将文件或目录移到垃圾箱、列出垃圾箱中的项目、从垃圾箱中恢复项目以及清空垃圾箱。

命令

  • trash-put:将文件或目录移动到垃圾箱。
  • trash-list:列出垃圾箱中的项目。
  • trash-restore:从垃圾箱中恢复项目。
  • trash-empty:清空垃圾箱。可以指定天数来清除超过该天数的垃圾箱内容。
  • trash-rm: 删除回收站文件

安装trash-cli
1、在线安装,可以通过包管理器安装。

(base) [root@bogon ~]# pip install trash-cli

在这里插入图片描述

2、离线源码安装

(base) [root@bogon trash-cli]# git clone https://github.com/andreafrancia/trash-cli.git
(base) [root@bogon trash-cli]# cd trash-cli
(base) [root@bogon trash-cli]# pip install .
(base) [root@bogon trash-cli]# export PATH=~/.local/bin:"$PATH"

使用场景:这个工具特别适合那些想要避免意外永久删除文件的用户,尤其是在使用命令行操作时。通过将文件移到垃圾箱,用户可以有机会在最终决定删除之前复查文件。
建议把rm命令设置下别名

(base) [root@bogon trash-cli]# alias rm='echo "The alias has been set in the rm command. Use the trash-cli command line tool"; false'

定时清空回收站
可以利用crontab来实现下面命令每天0点清空回收站3天前的内容

0 0 * * * trash-empty 3

总之,trash-cli 是一个非常有用的工具,为命令行环境中的文件删除操作提供了一个安全层。

可能出现的问题
CentOS7 安装时出现了一个错误

Processing /root/trash-cli
Collecting psutil (from trash-cli==0.21.7.24)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/e1/b0/7276de53321c12981717490516b7e612364f2cb372ee8901bd4a66a000d7/psutil-5.8.0.tar.gz
    Complete output from command python setup.py egg_info:
    /usr/lib64/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'python_requires'
      warnings.warn(msg)
    /usr/lib64/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'long_description_content_type'
      warnings.warn(msg)
    error in psutil setup command: 'extras_require' must be a dictionary whose values are strings or lists of strings containing valid project/version requirement specifiers.
    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-n6gexX/psutil/
You are using pip version 8.1.2, however version 21.2.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

psutil 版本的问题,执行安装旧版本

pip install psutil==5.7.0
  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值