Search code examples
linuxfind

Select files before today with GNU find


I'm trying to select files created before today with GNU find but it does not work as I expect :

$ find log/ -maxdepth 1 ! -newermt today -name "BACKUP*OK"
log/BACKUP_copro-20230504_OK
log/BACKUP_distrib-20230503_OK
log/BACKUP_distrib-20230504_OK
$ find log/ -maxdepth 1 -daystart ! -newermt today -name "BACKUP*OK"
log/BACKUP_copro-20230504_OK
log/BACKUP_distrib-20230503_OK
log/BACKUP_distrib-20230504_OK
$

But I expect this file only :

log/BACKUP_distrib-20230503_OK

What am I doing wrong ?


Solution

  • man find:

    -newerXY reference
    Succeeds if timestamp X of the file being considered is newer than timestamp Y of the file reference. The letters X and Y can be any of the following letters:

    • a The access time of the file reference
    • B The birth time of the file reference
    • c The inode status change time of reference
    • m The modification time of the file reference
    • t reference is interpreted directly as a time

    Some combinations are invalid; for example, it is invalid for X to be t. Some combinations are not implemented on all systems; for example B is not supported on all systems. If an invalid or unsupported combination of XY is specified, a fatal error results. Time specifications are interpreted as for the argument to the -d option of GNU date. If you try to use the birth time of a reference file, and the birth time cannot be determined, a fatal error message results. If you specify a test which refers to the birth time of files being examined, this test will fail for any files where the birth time is unknown.

    The important part is the last part: »Time specifications are interpreted as for the argument to the -d option of GNU date

    Let's try:

    $ date -d today
    Thu May  4 17:25:19 CEST 2023
    

    date -d today will print the date AND time. 00:00 today or 0 today sets the time part to 00:00.

    find '!' -newermt '00:00 today'