73

find has good support for finding files the more modified less than X days ago, but how can I use find to locate all files modified before a certain date?

I can't find anything in the find man page to do this, only to compare against another files time or to check for differences between created time and now. Is making a file with the desired time and comparing against that the only way to do this?

4
  • 2
    This would be better asked on SuperUser.com Commented Mar 16, 2010 at 11:30
  • 8
    I'm not going to close this: could be interesting for sysadmins as well.
    – splattne
    Commented Mar 16, 2010 at 13:41
  • The command is for part of a backup script, which grabs everything from /etc that was changed post-installation in our nightly backups.
    – DrStalker
    Commented Mar 18, 2010 at 1:05
  • 5
    Nine years old and I just noticed when moderating a new answer: the title and the body of this question do not state the same. The title asks for 'files older than <date>' but the body states 'modified after a certain date'. I interpret 'after' as newer than a specific date, not older.
    – Joffrey
    Commented Jul 11, 2019 at 21:33

11 Answers 11

38

If you have only '-newer file' then you can use this workaround:

# create 'some_file' having a creation date of 16 Mar 2010:
touch -t 201003160120 some_file

# find all files created after this date
find . -newer some_file

man touch:

  -t STAMP
          use [[CC]YY]MMDDhhmm[.ss] instead of current time

Assuming that your touch has this option (mine is touch 5.97).

1
  • 18
    If you're searching for -older counterpart, which does not exist: Just negate the expression with find . ! -newer some_file ! -name some_file. Second condition is necessary if you really want older files than the specified some_file.
    – robsch
    Commented Oct 16, 2020 at 9:02
85

No, you can use a date/time string.

From man find:

-newerXY reference
Compares the timestamp of the current file with reference. The reference argument is normally the name of a file (and one of its timestamps is used for the comparison) but it may also be a string describing an absolute time. X and Y are placeholders for other letters, and these letters select which time belonging to how reference is used for the comparison.

          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

Example:

find -newermt "mar 03, 2010" -ls
find -newermt yesterday -ls
find -newermt "mar 03, 2010 09:00" -not -newermt "mar 11, 2010" -ls
3
  • 5
    I'm using Centos 5.2, which uses GNU find version 4.2.27, and it does not support -newermt or any -newer parameter other than "-newer file"
    – DrStalker
    Commented Mar 16, 2010 at 7:08
  • 1
    GNU findutils 4.3.3 and newer supports -newerXY according to savannah.gnu.org/bugs/?11668 Commented Jun 29, 2010 at 13:58
  • 4
    -not -newer was what I was looking for Commented Jan 6, 2022 at 19:10
50

Not directly related to question, but might be interesting for some that stumble here.

find command doesn't directly support -older parameter for finding files older than some required date, but you can use negate statement (using accepted answer example):

touch -t 201003160120 some_file
find . ! -newer some_file

will return files older than provided date.

3
  • 3
    editors - maybe it should be merged into accepted answer?
    – nEJC
    Commented Sep 6, 2012 at 12:18
  • 1
    this solution is the most elegant way to solve this kind of issue :)
    – Labynocle
    Commented Jan 21, 2015 at 15:30
  • 2
    Using this idea of negation and avoiding need to touch a file (and specify the no. of days directly instead): $ find . ! -mtime -2 -exec ls -l {} \; Commented Feb 10, 2021 at 15:29
19
find <dir> -mtime -20

this find command will find files modified within the last 20 days.

  • mtime -> modified (atime=accessed, ctime=created)
  • -20 -> lesst than 20 days old (20 exactly 20 days, +20 more than 20 days)

You acan add additional limitations like:

find <dir> -mtime -20 -name "*.txt"

the same as before, but only finds files ending with '.txt'.

9

Just to add on - you may even use two newermt arguments to search in a time interval:

find ! -newermt "apr 01 2007" -newermt "mar 01 2007" -ls

to find all files from march 2007.

6
find ! -newermt “<DATE>”

like this:

find ! -newermt “jan 01 2015” 

This will give you files older than 01-01-2015 in your current directory.

https://muzaffarmahmoodblog.wordpress.com/2019/07/11/linux-command-to-remove-files-older-than-2015-in-a-directory/

1
  • I was about to write a comment welcoming you on ServerFault and asking you to reconsider posing a duplicate answer until I realized the question title and body say different things: the title asks for 'files older than <date>' and the question states 'modified after a certain date', which in my opinion are two different things. Maybe you could in your post clarify the difference and explain the use of the negate. Thank you for contributing and I will post a comment on the question for clarification. Thank you for contributing!
    – Joffrey
    Commented Jul 11, 2019 at 21:30
3
find <dir> -mtime +20

Is the correct answer to this question (find files modified before the last 20 days).

1
  • Actually it's not. The question explicitly asked to find files older than a date, not a number of days. Commented Mar 19, 2021 at 15:12
2

i hope this will help you

# find files modified last 2 days
find <directory> -type f -mtime -2

# find files modifies BEFORE last 2 days (add the negative char !)
find <directory> -type f ! -mtime -2

You can pipe whith xargs ls -trdlh to list you files

find <directory> -type f ! -mtime -2 | xargs ls -trdlh
1

You could use a script like this

#!/bin/bash

if [ $# -ne 1 ];then
  when="today"
else
  when=` date -d "$1" +"%s" `
fi
now=`date +"%s"`

seconds=`echo "$when - $now" | bc`
minutes=`echo "$seconds / 60 " | bc `

find . -cmin $minutes -print

Save it in your $PATH as "newerthan" and make it executable.

Then you can find file modified after a certain date like this:

newerthan "2010-03-10"

or

newerthan "last year"

or

newerthan "yesterday"

That should do what you want. I don't think there is a built in way to achieve this otherwise.

2
  • It's not necessary to use bc for integer math, Bash can do it: ((seconds = when - now)), the format argument to date should have a plus date +%s and some systems may not have date -d Commented Mar 16, 2010 at 11:02
  • This is for Centos 5.2. Which does have -d. Useful to know about the integer maths though. Commented Mar 16, 2010 at 11:29
0

if your date is formatted such that its ok for comparison,

mydate=201003160120
find . -type f -printf "%AY%Am%Ad%AH%AM%AS/:%p\n" | awk -vd="$mydate" -F'/:' '$1 > d{ print $2 }'
0

If you search for files created before today

TODAYMIDNIGHT=$(date --date '1 day ago' +"%Y-%m-%d 23:59:59")
find . -type f -not -newermt "$TODAYMIDNIGHT"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .