Explaining the 'find -mtime' command

后端 未结 4 1422
时光说笑
时光说笑 2020-12-09 15:30

I\'m trying to remove all the dateed logs except the most recent. Before I execute a script to remove the files, I want to of course test my commands to make sure I\'m bring

相关标签:
4条回答
  • 2020-12-09 16:12

    To find all files modified in the last 24 hours use the one below. The -1 here means changed 1 day or less ago.

    find . -mtime -1 -ls
    
    0 讨论(0)
  • 2020-12-09 16:18

    +1 means 2 days ago. It's rounded.

    0 讨论(0)
  • 2020-12-09 16:22

    The POSIX specification for find says:

    -mtimen The primary shall evaluate as true if the file modification time subtracted from the initialization time, divided by 86400 (with any remainder discarded), is n.

    Interestingly, the description of find does not further specify 'initialization time'. It is probably, though, the time when find is initialized (run).

    In the descriptions, wherever n is used as a primary argument, it shall be interpreted as a decimal integer optionally preceded by a plus ( '+' ) or minus-sign ( '-' ) sign, as follows:

    +n More than n.
      n Exactly n.
    -n Less than n.

    At the given time (2014-09-01 00:53:44 -4:00, where I'm deducing that AST is Atlantic Standard Time, and therefore the time zone offset from UTC is -4:00 in ISO 8601 but +4:00 in ISO 9945 (POSIX), but it doesn't matter all that much):

    1409547224 = 2014-09-01 00:53:44 -04:00
    1409457540 = 2014-08-30 23:59:00 -04:00
    

    so:

    1409547224 - 1409457540 = 89684
    89684 / 86400 = 1
    

    Even if the 'seconds since the epoch' values are wrong, the relative values are correct (for some time zone somewhere in the world, they are correct).

    The n value calculated for the 2014-08-30 log file therefore is exactly 1 (the calculation is done with integer arithmetic), and the +1 rejects it because it is strictly a > 1 comparison (and not >= 1).

    0 讨论(0)
  • 2020-12-09 16:26

    You can use the find command with mtime.

    For example:

     find /path of the folder -type f -mtime +30
    

    -type refers to file type.

    +30 refers to the date before the present date.

    For more options we can refer to the man page.

    0 讨论(0)
提交回复
热议问题