菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
498
0

CentOS 6 回收站工具

原创
05/13 14:22
阅读数 75823

近日在操作CentOS时,一个不留神,误删除了一个文件夹。幸好有备份,才避免了损失。痛定思痛,未雨绸缪,是时候考虑CentOS环境下的后悔药了。

网上有使用debugfs工具来进行删除恢复的,比较复杂,且和文件系统格式有关,有局限。

有使用shell脚本实现回收站的,没点儿shell功底,看不懂。也不愿花时间研究了,对我来讲,应用这个方式不那么得心应手。

比较下来,我选择了linux-stuff saferm.sh的方式,简单有效。开始上干货:

安装Git

 # yum install git
 # git --version

clone

# git clone https://github.com/lagerspetz/linux-stuff.git

若因为服务器网络限制等原因,不成功,可以直接在客户端浏览器里访问 saferm,Download ZIP,然后上传服务器。

有必要先看下README.md

[root@ox2 linux-stuff]# cd linux-stuff-master
[root@ox2 linux-stuff-master]# ls
firefox-dark-theme  gnome                   legacy       README.md  sources.list.d
games               hybrid-graphics-and-pm  metapackage  scripts
[root@ox2 linux-stuff-master]# vim README.md
[root@ox2 linux-stuff-master]#

解压、安装

[root@ox2 linux-stuff]# unzip linux-stuff-master.zip
Archive:  linux-stuff-master.zip
eb8649c4e85823595aaf37c63bcccc7e54eee06a
   creating: linux-stuff-master/
  inflating: linux-stuff-master/README.md
   creating: linux-stuff-master/firefox-dark-theme/
  inflating: linux-stuff-master/firefox-dark-theme/userContent.css
   creating: linux-stuff-master/games/
   creating: linux-stuff-master/games/Zero-K/
  inflating: linux-stuff-master/games/Zero-K/Zero-K.png
...

[root@ox2 linux-stuff]# ll
total 1264
drwxr-xr-x. 10 root root    4096 Mar  6 23:54 linux-stuff-master
-rw-r--r--.  1 root root 1289641 Apr 26 08:58 linux-stuff-master.zip
[root@ox2 linux-stuff]# mv linux-stuff-master/scripts/saferm.sh  /usr/bin
[root@ox2 linux-stuff]# vim /etc/bashrc
添加rm别名:
alias rm='saferm.sh'

[root@ox2 linux-stuff]# source /etc/bashrc

其实各环境加载配置文件均可添加,按您的思路来。附区别如下:

/etc/profile: 此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行。并从/etc/profile.d目录的配置文件中搜集shell的设置。
/etc/bashrc: 为每一个运行bash shell的用户执行此文件。当bash shell被打开时,该文件被读取。
~/.bash_profile: 每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该文件仅仅执行一次!默认情况下,他设置一些环境变量,执行用户的.bashrc文件。
~/.bashrc: 该文件包含专用于你的bash shellbash信息,当登录时以及每次打开新的shell时,该该文件被读取。
~/.bash_logout: 当每次退出系统(退出bash shell)时,执行该文件。

另外,/etc/profile中设定的变量(全局)的可以作用于任何用户,而~/.bashrc等中设定的变量(局部)只能继承/etc/profile中的变量,他们是“父子”关系。

查看saferm帮助

[root@ox2 linux-stuff-master]# saferm.sh -h
This is saferm.sh 1.16. LXDE and Gnome3 detection.
    Will ask to unsafe-delete instead of cross-fs move. Allows unsafe (regular rm) delete (ignores trashinfo).
    Creates trash and trashinfo directories if they do not exist. Handles symbolic link deletion.
    Does not complain about different user any more.

Usage: /path/to/saferm.sh [OPTIONS] [--] files and dirs to safely remove
OPTIONS:
-r      allows recursively removing directories.
-f      Allow deleting special files (devices, ...).
-u      Unsafe mode, bypass trash and delete files permanently.
-v      Verbose, prints more messages. Default in this version.
-q      Quiet mode. Opposite of verbose.

[root@ox2 linux-stuff-master]#

测试验证

[root@ox2 linux-stuff]# touch test_file1 test_file2
[root@ox2 linux-stuff]# ll
total 1264
drwxr-xr-x. 10 root root    4096 Mar  6 23:54 linux-stuff-master
-rw-r--r--.  1 root root 1289641 Apr 26 08:58 linux-stuff-master.zip
-rw-r--r--.  1 root root       0 Apr 26 09:06 test_file1
-rw-r--r--.  1 root root       0 Apr 26 09:06 test_file2
[root@ox2 linux-stuff]# rm 0rf test_file1
File does not exist: 0rf.
Moving test_file1 to /root/Trash
[root@ox2 linux-stuff]# ll /root/Trash/
total 0
-rw-r--r--. 1 root root 0 Apr 26 09:06 test_file1
[root@ox2 linux-stuff]# mkdir test_dir
[root@ox2 linux-stuff]# touch test_dir/test_file1
[root@ox2 linux-stuff]# ls test_dir/test_file1
test_dir/test_file1
[root@ox2 linux-stuff]# rm -rf test_dir
Moving test_dir to /root/Trash
[root@ox2 linux-stuff]# ll /root/Trash/
total 4
drwxr-xr-x. 2 root root 4096 Apr 26 09:07 test_dir
-rw-r--r--. 1 root root    0 Apr 26 09:06 test_file1
[root@ox2 linux-stuff]# ll /root/Trash/test_dir/
total 0
-rw-r--r--. 1 root root 0 Apr 26 09:07 test_file1
[root@ox2 linux-stuff]# 

测试删除重名文件,先删除一个test_file1,再删除一个test_file1。~/Trash里先删除的test_file1,会自动改名为test_file1~。

如果先后删除了3个名为test_file1的文件,那最早删除的那个test_file1就会被Trash丢弃了。

[root@ox2 linux-stuff]# vim test_file1  # 添加内容“test conent”
[root@ox2 linux-stuff]# rm test_file1
Moving test_file1 to /root/Trash
[root@ox2 linux-stuff]# ll /root/Trash/
total 8
drwxr-xr-x. 2 root root 4096 Apr 26 09:07 test_dir
-rw-r--r--. 1 root root   13 Apr 26 09:08 test_file1
-rw-r--r--. 1 root root    0 Apr 26 09:06 test_file1~
[root@ox2 linux-stuff]# cat /root/Trash/test_file1 /root/Trash/test_file1~
test conent

[root@ox2 linux-stuff]# vim test_file1
[root@ox2 linux-stuff]# cat test_file1
test2 conent
[root@ox2 linux-stuff]# rm test_file1
Moving test_file1 to /root/Trash
[root@ox2 linux-stuff]# ll /root/Trash
total 12
drwxr-xr-x. 2 root root 4096 Apr 26 09:07 test_dir
-rw-r--r--. 1 root root   13 Apr 26 09:10 test_file1
-rw-r--r--. 1 root root   13 Apr 26 09:08 test_file1~
[root@ox2 linux-stuff]# cat /root/Trash/test_file1 /root/Trash/test_file1~
test2 conent
test conent

[root@ox2 linux-stuff]# ls
linux-stuff-master  linux-stuff-master.zip  test_file2
[root@ox2 linux-stuff]#

换用户测试

[oracle@ox2 oinstall]$ mkdir rm_test
[oracle@ox2 oinstall]$ cd rm_test/
[oracle@ox2 rm_test]$ vim rm_test.txt
[oracle@ox2 rm_test]$ cd ..
[oracle@ox2 oinstall]$ rm  rm_test
Is a directory (and -r not specified): rm_test.
[oracle@ox2 oinstall]$ rm -rf rm_test
Moving rm_test to /home/oracle/Trash
[oracle@ox2 oinstall]$ ll ~/Trash/
total 4
drwxr-xr-x. 2 oracle oinstall 4096 Apr 26 09:17 rm_test
[oracle@ox2 oinstall]$ cat ~/Trash/rm_test/rm_test.txt
锦帽貂裘千骑卷平岗

[oracle@ox2 oinstall]$

该方案也存在自身问题,似乎没有记录删除文件的原始所在路径。

README.md中的英文没有仔细研究,欢迎各位兄台交流指正,感谢分享。

发表评论

0/200
498 点赞
0 评论
收藏