zoukankan      html  css  js  c++  java
  • centos rm 回收站

    桌面版本的linux就默认有,非桌面版本貌似没(反正我没找到)

    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目录位置觉得不爽,可以修改环境变量,改成其他位置,注意保证目录可写。

  • 相关阅读:
    Codeforces Round #344 (Div. 2) C. Report 其他
    Codeforces Round #344 (Div. 2) B. Print Check 水题
    Codeforces Round #344 (Div. 2) A. Interview 水题
    8VC Venture Cup 2016
    CDOJ 1280 772002画马尾 每周一题 div1 矩阵快速幂 中二版
    CDOJ 1280 772002画马尾 每周一题 div1 矩阵快速幂
    CDOJ 1279 班委选举 每周一题 div2 暴力
    每周算法讲堂 快速幂
    8VC Venture Cup 2016
    Educational Codeforces Round 9 F. Magic Matrix 最小生成树
  • 原文地址:https://www.cnblogs.com/yaohaitao/p/5790134.html
Copyright © 2011-2022 走看看