how do I check in bash whether a file was created more than x time ago?

后端 未结 8 1233
时光说笑
时光说笑 2020-12-22 18:24

I want to check in linux bash whether a file was created more than x time ago.

let\'s say the file is called text.txt and the time is 2 hours.

 if [         


        
8条回答
  •  半阙折子戏
    2020-12-22 19:20

    Consider the outcome of the tool 'stat':

      File: `infolog.txt'
      Size: 694         Blocks: 8          IO Block: 4096   regular file
    Device: 801h/2049d  Inode: 11635578    Links: 1
    Access: (0644/-rw-r--r--)  Uid: ( 1000/     fdr)   Gid: ( 1000/     fdr)
    Access: 2009-01-01 22:04:15.000000000 -0800
    Modify: 2009-01-01 22:05:05.000000000 -0800
    Change: 2009-01-01 22:05:05.000000000 -0800
    

    You can see here the three dates for Access/modify/change. There is no created date. You can only really be sure when the file contents were modified (the "modify" field) or its inode changed (the "change" field).

    Examples of when both fields get updated:

    "Modify" will be updated if someone concatenated extra information to the end of the file.

    "Change" will be updated if someone changed permissions via chmod.

提交回复
热议问题