# Linux find files relative to a date

# What date are we talking about ?

There is 3 timestamps that are recorded by the filesystem for each file and folder:

  • mtime (modification time): last time the file content changed
  • atime (access time): last time the file was accessed
  • ctime (change time): last time the file changed; take into account modifications on file content and metadata, like file owner and file permission

WARNING

Linux filesystems are not required to store the creation time (but some does, however, like ext4). That's why most linux command don't include creation time based options.

So, when you want to find file relative to a date, you have to specify which date you're talking about.

There is several options that can be used to find files and folders relative to a date. In the following, X and Y must be replaced by a, m or c, depending on which file time you're referring to.

Options Description
-Xtime n compare the files X time to a given number of n days from the current time
-Xmin m compare the files X time to a given number of m minutes from the current time
-newerXt time compare the files X time to a given time
-newerXY file compare the files X time to the Y time of another file

# Find files based on the last n days

With the -Xtime and -Xmin options, you can either look for files with their X time in the last n days or before the last n days by prepending - or + respectively to the number of days:

  • Find files in the current directory modified in the last n days
> find . -mtime -2 days # n = 2
1
  • Find files in the current directory accessed before the last n days
> find . -atime +2 days # n = 2
1

To compute the exact timestamp used for the comparison, find substract the file timestamp to the current timestamp and compare the result mod 24 to n.

TIP

It's also possible to use the start of current day instead of the current timestamp with the -daystart positional option:

> find . -daystart -mtime +2 days # n = 2
1

# Find files based on a given date

The -newerXt option allows to find files with an X time before or after a given date:

  • Find files in the current directory modified after a given date
> find . -newermt '2019-05-28 23:00'
1
  • Find files in the current directory changed before a given date
> find . ! -newerct '2019-05-28 23:00'
1

TIP

In find the ! (or -not) operator can be used to invert the result of the following test.

  • Find files in the current directory modified between two dates: simply combine tests
> find . -newermt '2019-05-28 23:00' -not -newermt '2019-05-29 23:00'
1

# Find based on another file time

  • Find files in the current directory modified after the ./file1 was last accessed
> find . -newerma ./file1
1

# Creation date (Birth date)

If your filesystem records the creation date, X and Y can be replaced by B to test the file birth date:

  • Find files in the current directory created before a given date
> find . ! -newerBt '2019-05-28 23:00'
1

If you're filesystem doesn't support the birth date, find fails with the message:

find: This system does not provide a way to find the birth time of a file.
find: invalid predicate `-newerBt'
1
2
Last updated: 6/11/2019, 11:01:06 PM