操作系统 
首页 > 操作系统 > 浏览文章

CentOS增加系统回收站功能介绍

(编辑:jimmy 日期: 2024/10/3 浏览:3 次 )

linux rm删除文件之后,恢复就比较麻烦了,即使恢复了,文件名格式都变成一串数字了。

修改root用户的环境变量

vi ~/.bashrc

注释第5行的别名

#alias rm='rm -i'

最后一行添加如下内容

mkdir -p ~/.trash

alias rm=trash

alias r=trash

alias rl='ls ~/.trash'

alias ur=undelfile

undelfile()

{

  mv -i ~/.trash/$@ ./

}

trash()

{

  mv $@ ~/.trash/

}

cleartrash()

{

    read -p "clear sure?[n]" confirm

    [ $confirm == 'y' ] || [ $confirm == 'Y' ]  && /bin/rm -rf ~/.trash/*

}

重新加载环境变量

source ~/.bashrc

使用命令ll -a查看目录,发现多了目录.trash,这个目录是用来存在删除的文件

drwxr-xr-x.  2 root root       4096 Jun  4 11:31 .trash

删除一个文件

[root@localhost ~]# rm percona-xtrabackup_2.2.3.orig.tar.gz

查看目录,发现删除的文件在回收站目录

[root@localhost ~]# ll .trash/

total 33780

-rw-r--r--. 1 root root 34584359 Jun  2 09:39 percona-xtrabackup_2.2.3.orig.tar.gz

如果需要清空回收站文件

使用以下命令

[root@localhost ~]# cleartrash

clear sure?[n]y

再次查看,发现空了。

[root@localhost ~]# ll .trash/

total 0

虽然rm用别名定义了,但是可以是用绝对路径删除文件

比如/bin/rm 1.txt

它是不会保存到.trash目录的。

如果需要定义自动清理7天删除的文件

可以写一个脚本

#!/bin/bash

find /root/.trash -ctime 7 -type f -name "*" -exec /bin/rm {} \;

然后在任务计划里面调用一下就可以了。

如果Linux除了root用户,还有其他用户需要登陆服务器,也想他们使用回收站机制

可以修改系统环境变量

vi /etc/profile

最后一行添加

mkdir -p ~/.trash

alias rm=trash

alias r=trash

alias rl='ls ~/.trash'

alias ur=undelfile

undelfile()

{

  mv -i ~/.trash/$@ ./

}

trash()

{

  mv $@ ~/.trash/

}

 

cleartrash()

{

    read -p "clear sure?[n]" confirm

    [ $confirm == 'y' ] || [ $confirm == 'Y' ]  && /bin/rm -rf ~/.trash/*

}

重新加载环境变量

source /etc/profile

创建普通用户测试

useradd a

设置密码

passwd a

登陆Linux

查看目录,发现会创建.trash目录

[a@localhost ~]$ ll -a

total 24

drwx------. 3 a    a    4096 Jun  4 11:45 .

drwxr-xr-x. 5 root root 4096 Jun  4 11:44 ..

-rw-r--r--. 1 a    a      18 Oct 16  2014 .bash_logout

-rw-r--r--. 1 a    a     176 Oct 16  2014 .bash_profile

-rw-r--r--. 1 a    a     124 Oct 16  2014 .bashrc

drwxrwxr-x. 2 a    a    4096 Jun  4 11:45 .trash

创建一个空文件

[a@localhost ~]$ touch 1.txt

删除文件

[a@localhost ~]$ rm 1.txt

查看回收站目录,发现多了一个文件

[a@localhost ~]$ ll .trash/

total 0

-rw-rw-r--. 1 a a 0 Jun  4 11:45 1.txt

如果对.trash目录位置觉得不爽,可以修改环境变量,改成其他位置,注意保证目录可写。

上一篇:CentOS7下的软件安装方法及策略详解
下一篇:CentOS安装scp命令详解
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。