calculate total used disk space by files older than 180 days using find

前端 未结 5 1696
自闭症患者
自闭症患者 2020-12-25 12:21

I am trying to find the total disk space used by files older than 180 days in a particular directory. This is what I\'m using:

    find . -mtime +180 -exec d         


        
5条回答
  •  遥遥无期
    2020-12-25 13:05

    Be careful not to take into account the disk usage by the directories. For example, I have a lot of files in my ~/tmp directory:

    $ du -sh ~/tmp
    3,7G    /home/rpet/tmp
    

    Running the first part of example posted by devnull to find the files modified in the last 24 hours, we can see that awk will sum the whole disk usage of the ~/tmp directory:

    $ find ~/tmp -mtime 0 -exec du -ks {} \; | cut -f1
    3849848
    84
    80
    

    But there is only one file modified in that period of time, with very little disk usage:

    $ find ~/tmp -mtime 0
    /home/rpet/tmp
    /home/rpet/tmp/kk
    /home/rpet/tmp/kk/test.png
    
    $ du -sh ~/tmp/kk
    84K /home/rpet/tmp/kk
    

    So we need to take into account only the files and exclude the directories:

    $ find ~/tmp -type f -mtime 0 -exec du -ks {} \; | cut -f1 | awk '{total=total+$1}END{print total/1024}'
    0.078125
    

    You can also specify date ranges using the -newermt parameter. For example:

    $ find . -type f -newermt "2014-01-01" ! -newermt "2014-06-01"
    

    See http://www.commandlinefu.com/commands/view/8721/find-files-in-a-date-range

提交回复
热议问题