zoukankan      html  css  js  c++  java
  • Linux/Unix 怎样找出并删除某一时间点的文件

    Linux/Unix 怎样找出并删除某一时间点的文件

    在Linux/Unix系统中,我们的应用每天会产生日志文件,每天也会备份应用程序和数据库,日志文件和备份文件长时间积累会占用大量的存储空间,而有些日志和备份文件是不需要长时间保留的,一般保留7天内的文件即可,那么我们怎么找出并删除7天前产生的日志文件和备份文件并将其删除呢?

    Linux/Unix提供了find 操作系统命令,使用该命令可以实现我们的目标。
    $man find 可以查看find命令的使用方法。
    1. 找出 n 天前的文件

    $find /temp/ -type f -mtime +n -print

    注:/temp/ 指出寻找/temp/目录下的文件
    -type f 指出找系统普通文件,不包含目录文件
    -mtime +n 指出找 n*24 小时前的文件
    -print 将找出的文件打印出来
    如:找出 7 天前的文件

    $find /temp/ -type f -mtime +7 -print

    找出 3 天前的文件

    find /temp/ -type f -mtime +3 -print

    2. 找出并删除 7 天前的文件

    $find /temp/ -type f -mtime +7 -print -exec rm -f {} ;

    注:-exec 指出要执行后面的系统命令
    rm -f 删除找出的文件
    {} 只有该符号能跟在命令后面
    结束符
    3. 也可以使用 xargs 代替 -exec

    $find /temp/ -type f -mtime +7 -print | xargs rm -f

    find命令用途举例:
    如:
    * 查找/var下最大的前10个文件:

    $ find /var -type f -ls | sort -k 7 -r -n | head -10

    * 查找/var/log/下大于5GB的文件:

    $ find /var/log/ -type f -size +5120M -exec ls -lh {} ;

    * 找出今天的所有文件并将它们拷贝到另一个目录:

    $ find /home/me/files -ctime 0 -print -exec cp {} /mnt/backup/{} ;

    * 找出所有一周前的临时文件并删除:

    $ find /temp/ -mtime +7-type f | xargs /bin/rm -f

    * 查找所有的mp3文件,并修改所有的大写字母为小写字母:

    $ find /home/me/music/ -type f -name *.mp3 -exec rename 'y/[A-Z]/[a-z]/' '{}' ;
  • 相关阅读:
    Codeforces Round #619 (Div. 2) ABC 题解
    Codeforces Round #669 ABC 题解
    中大ACM个人赛 ABC题题解
    Codeforces Round #601 (Div. 2) ABC 题解
    SCAU 2019年校赛 部分题解
    SCAU 2018年新生赛 初出茅庐 全题解
    Educational Codeforces Round 74 (Rated for Div. 2) ABC 题解
    Codeforces Round #603 (Div. 2) ABC 题解
    【题解】 CF767E Change-free 带悔贪心
    【题解】 NOIp2013 华容道 最短路+状态压缩 Luogu1979
  • 原文地址:https://www.cnblogs.com/luckyall/p/6940477.html
Copyright © 2011-2022 走看看