linux怎么看垃圾回收情况,Linux如何模拟垃圾回收站功能

我们知道Windows系统有回收站的功能,正确设置后,当用户删除文件或文件夹时,操作系统会将这些“删除”的文件或文件夹放到回收站中,而并没有真正意义上的删除文件。其实Linux系统中也可以模拟这样的功能。下面介绍一下GitHub上的一个非常有意思的项目,里面有个脚本Saferm.sh可以模拟这种功能。关于Saferm.sh的介绍如下,更多详细信息参考https://github.com/lagerspetz/linux-stuff

This repo contains useful linux scripts. No guarantee that they work or warranty of any kind is given. Some highlights:

Saferm.sh

·scripts/saferm.sh: alias this to "rm". Moves files to your desktop environment's trash folder instead of permanently deleting files when you type "rm".

·scripts/manually-installed.sh: Shows the list of manually installed (deb) packages on the system. Useful for keeping track of what is installed.

·scripts-manually-installed-deps.sh: Shows which packages are manually installed, but do not need to be, because they are being pulled as dependencies by other packages. With the -a flag marks these manually installed dependencies as automatically installed.

安装

安装方式非常简单,就是将saferm.sh这个脚本拷贝到/bin目录下面,下面测试环境为CentOS Linux release 7.5.1804 (Core)

# git clone https://github.com/lagerspetz/linux-stuff# mv linux-stuff/scripts/saferm.sh /bin

配置

找到.bashrc文件,修改或增加一行alias rm=saferm.sh。关于bashrc,它用于保存用户的环境信息,bashrc用于交互式non-loginshell。每个可登陆用户的目录下都有.bashrc这样一个隐藏文件。

[root@KerryDB tmp]# find / -name ".bashrc"/home/postgres/.bashrc/etc/skel/.bashrc/root/.bashrc[root@KerryDB tmp]# more /root/.bashrc# .bashrc# User specific aliases and functionsalias rm='rm -i'alias cp='cp -i'alias mv='mv -i'# Source global definitionsif [ -f /etc/bashrc ]; then. /etc/bashrcfi

修改/root/.bashrc文件,修改或增加一行配置

#alias rm='rm -i'

alias rm=saferm.sh

测试

执行source .bashrc,让环境变量生效,然后我们简单测试,测试验证其已经生效

[root@KerryDB tmp]# source /root/.bashrc[root@KerryDB tmp]# lslinux-stuff[root@KerryDB tmp]# rm -rf linux-stuff/Moving linux-stuff/ to /root/Trash[root@KerryDB tmp]#

c1f867856a35160487f1eb73ec072962.png

如上所示,我修改/root/.bashrc这个文件,在root账号下删除文件或文件夹时,系统将其移动到/root/Trash下面。那么此时在postgres用户下测试,就会发现文件直接被删除了,并没有将其放到“回收站”。这个是因为我们没有设置postgres用户家目录下的.bashrc(/home/postgres/.bashrc),所以,如果要对每个用户都生效,有两种解决方案:

1:修改每个用户家目录下的.bashrc文件,修改其私有环境变量。

2:修改/etc/bashrc文件,这个是是系统全局环境变量设定

例如,我们修改/etc/bashrc后,执行source /etc/bashrc使其生效后,测试发现在postgres用户下也会将删除的文件移动到/home/postgres/Trash下了。

[root@KerryDB ~]# su - postgresLast login: Thu May 21 15:48:50 +08 2020 on pts/1[postgres@KerryDB ~]$ cat >test.txtit's only a test^C[postgres@KerryDB ~]$ rm test.txtMoving test.txt to /home/postgres/Trash[postgres@KerryDB ~]$

其实/etc/bashrc是系统全局环境变量设定,~/.bashrc用户家目录下的私有环境变量设定。这里不做展开介绍。

你会发现“回收站”目录在各个用户的家目录下,文件夹名Trash,其实这个是可以修改配置的,因为saferm.sh里面就是这样设定的。当然也可以修改。

ca9ed97d9ba74045e5a2cbbca5b66ef0.png

BUG和问题

另外,就是发现saferm.sh确实也是有bug的。并不能100%的保证任何被删除的文件都会放入“回收站”,例如下面例子所示

[root@KerryDB log]# ls -lrt mail*-rw------- 1 root root 0 Apr 19 03:31 maillog-20200426-rw------- 1 root root 0 Apr 26 03:41 maillog-20200503-rw------- 1 root root 0 May  3 03:22 maillog-20200510-rw------- 1 root root 0 May 10 03:17 maillog-20200517-rw------- 1 root root 0 May 17 03:39 maillog[root@KerryDB log]# find /var/log -mtime +7 -name "maillog*" -exec rm -rf {} ;[root@KerryDB log]# ls /root/Trash/linux-stuff

这种方式删除的文件直接被删除了(这个也是偶然一次操作测试发现的),并没有移动到回收站下面。

定期清理“回收站”

如果一直不清理回收站,那么就有可能出现磁盘空间告警的情况,正确的做法是配置crontab作业,定期清空“垃圾回收站”。例如类似这样的设置

0 * * * 6 find /root/Trash/ -mtime +7 -name "*" -exec rm -rf {} ;

参考资料:

https://github.com/lagerspetz/linux-stuff/blob/master/scripts/saferm.sh

https://www.linuxprobe.com/rm-saferm-trash.html

内容来源于网络如有侵权请私信删除

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值