运维中, 删除文件都要慎重, 所以, windows的回收站功能有时候也是非常有必要的.
trash是一个项目, 可以实现类似windows的回收站功能. 只要我们安装了trash, 将rm重新建立别名, 就能用trash来实现文件的安全删除和恢复.
第一步: 安装
[root@localhost ~]# easy_install trash-cli
Searching for trash-cli
Reading https://pypi.python.org/simple/trash-cli/
Best match: trash-cli 0.17.1.14
Downloading https://files.pythonhosted.org/packages/d5/a2/2be332ce279d6889adb69dd71172ac8f48fd441900a15546253287638f1e/trash-cli-0.17.1.14.tar.gz#sha256=b0a06ca15d05e70de00f4c19a79ec677f4ae9f29e20d6d3e09c2494a00650007
Processing trash-cli-0.17.1.14.tar.gz
Writing /tmp/easy_install-LYt4fV/trash-cli-0.17.1.14/setup.cfg
Running trash-cli-0.17.1.14/setup.py -q bdist_egg --dist-dir /tmp/easy_install-LYt4fV/trash-cli-0.17.1.14/egg-dist-tmp-6Cu40t
zip_safe flag not set; analyzing archive contents...
Adding trash-cli 0.17.1.14-r0 to easy-install.pth file
Installing trash-put script to /usr/bin
Installing trash-rm script to /usr/bin
Installing trash-list script to /usr/bin
Installing trash-restore script to /usr/bin
Installing trash-empty script to /usr/bin
Installing trash script to /usr/bin
Installed /usr/lib/python2.7/site-packages/trash_cli-0.17.1.14_r0-py2.7.egg
Processing dependencies for trash-cli
Finished processing dependencies for trash-cli
第二步: 了解功能
trash
trash-put trashes files and directories, 删除文件目录
trash-empty empty the trashcan(s), 清空回收站
trash-list list trashed file, 列出删除的文件
trash-restore restore a trashed file, 重新恢复删除的文件到一个目录
trash-rm remove individual files from trash can, 删除回收站的个别文件
第三步: 测试各个功能
[root@localhost ~]# mkdir test # 创建测试环境
[root@localhost ~]# cd test
[root@localhost test]# touch {1,2,3,4}.txt
[root@localhost test]# ls
1.txt 2.txt 3.txt 4.txt
[root@localhost test]# mkdir test_dir
[root@localhost test]# ls
1.txt 2.txt 3.txt 4.txt test_dir
[root@localhost test]# trash 1.txt # 删除文件
[root@localhost test]# ls
2.txt 3.txt 4.txt test_dir
[root@localhost test]# trash test_dir/ # 删除目录
[root@localhost test]# ls
2.txt 3.txt 4.txt
[root@localhost test]# trash-list # 查看回收站
2018-07-31 18:42:49 /root/test/1.txt
2018-07-31 18:42:54 /root/test/test_dir
[root@localhost test]# trash-empty # 清空回收站
[root@localhost test]# ls
2.txt 3.txt 4.txt
[root@localhost test]# trash-list
空....
[root@localhost test]# trash * # 删除所有文件
[root@localhost test]# trash-list # 查看删除后的列表
2018-07-31 18:47:37 /root/test/2.txt
2018-07-31 18:47:37 /root/test/3.txt
2018-07-31 18:47:37 /root/test/4.txt
[root@localhost test]# trash-restore # 交互式恢复文件
0 2018-07-31 18:47:37 /root/test/2.txt
1 2018-07-31 18:47:37 /root/test/3.txt
2 2018-07-31 18:47:37 /root/test/4.txt
What file to restore [0..2]: 0 # 输入id回车
[root@localhost test]# ls # 恢复成功
2.txt
[root@localhost test]# trash-list
2018-07-31 18:47:37 /root/test/3.txt
2018-07-31 18:47:37 /root/test/4.txt
[root@localhost test]# trash-rm 3.txt # 删除单个文件
[root@localhost test]# trash-list
2018-07-31 18:47:37 /root/test/4.txt
第四步: 别名替换
alias rm='trash'
如此这般, 删除文件后就不会直接丢失.
后续的功能, 读者自己搞, 应该也能高出自己的花样. 这里就不赘述了.
转载请注明:liutianfeng.com » rm删除太危险, trash替换rm的方法
发表回复
要发表评论,您必须先登录。