302

How do I use the UNIX command find to search for files created on a specific date?

jchanger
  • 739
  • 10
  • 29
sverrejoh
  • 16,464
  • 13
  • 41
  • 29

9 Answers9

396

As pointed out by Max, you can't, but checking files modified or accessed is not all that hard. I wrote a tutorial about this, as late as today. The essence of which is to use -newerXY and ! -newerXY:

Example: To find all files modified on the 7th of June, 2007:

$ find . -type f -newermt 2007-06-07 ! -newermt 2007-06-08

To find all files accessed on the 29th of september, 2008:

$ find . -type f -newerat 2008-09-29 ! -newerat 2008-09-30

Or, files which had their permission changed on the same day:

$ find . -type f -newerct 2008-09-29 ! -newerct 2008-09-30

If you don't change permissions on the file, 'c' would normally correspond to the creation date, though.

AbsoluteƵERØ
  • 7,816
  • 2
  • 24
  • 35
Arve
  • 4,112
  • 1
  • 16
  • 4
  • 7
    My version of find (GNU 4.2.32) doesn't seem to support the -newerXY predicates. Is there a particular minimum version needed? Or is it a case of compiling find with a special configure switch? – yukondude Oct 01 '08 at 17:07
  • 1
    @yukondude: You're right. The version of find I have locally -- GNU 4.4.0 -- has it, while 4.1.20 that I have on Dreamhost doesn't. The kludge with creating two files should work in either, though. – Arve Oct 02 '08 at 07:45
  • 1
    On some systems (FreeBSD, OS/X...) `-newerBt` will match on the file's _birth_ (creation) time. – Stephane Chazelas Jul 21 '14 at 13:20
  • 3
    Note that the `-newerxt` is available on [FreeBSD](http://svnweb.freebsd.org/base?view=revision&revision=76250) since 2001 (where it was first provided [as a patch](http://markmail.org/thread/mvofenww57bai3vk) in 1998), a few other BSDs and GNU find (since 4.3.3 in 2007), based on HP/UX find, which introduced `-newerXY` (but where Y == t is not supported). – Stephane Chazelas Jul 21 '14 at 14:00
  • Using: find (GNU findutils) 4.4.0, this syntax does not work on SUSE ext3, if it's supposed to list files /w mtime on that date, it returns nothing – niken Oct 14 '15 at 17:04
  • 1
    Note that the exclamation point may need to be escaped (`\!`) if you have Bash history substitution enabled (which is the default). Or you can disable it with `set +H`. – Dennis Williamson Feb 06 '19 at 00:21
  • 1
    Linked tutorial (http://www.virtuelvis.com/2008/10/how-to-use-find-to-search-for-files-created-on-a-specific-date/) is dead – Dawid Moś Jan 31 '21 at 19:24
  • archive.org link of the mentioned tutorial: https://web.archive.org/web/20200216130020/http://virtuelvis.com/2008/10/how-to-use-find-to-search-for-files-created-on-a-specific-date/ – holzkohlengrill Nov 07 '22 at 15:17
84

Use this command to search for files and folders on /home/ add a time period of time according to your needs:

find /home/ -ctime time_period

Examples of time_period:

  • More than 30 days ago: -ctime +30

  • Less than 30 days ago: -ctime -30

  • Exactly 30 days ago: -ctime 30

galoget
  • 722
  • 9
  • 15
Chris
  • 2,984
  • 25
  • 24
53

It's two steps but I like to do it this way:

First create a file with a particular date/time. In this case, the file is 2008-10-01 at midnight

touch -t 0810010000 /tmp/t

Now we can find all files that are newer or older than the above file (going by file modified date).
You can also use -anewer for accessed and -cnewer file status changed.

find / -newer /tmp/t
find / -not -newer /tmp/t

You could also look at files between certain dates by creating two files with touch

touch -t 0810010000 /tmp/t1
touch -t 0810011000 /tmp/t2

This will find files between the two dates & times

find / -newer /tmp/t1 -and -not -newer /tmp/t2
Tms91
  • 3,456
  • 6
  • 40
  • 74
Mark Biek
  • 146,731
  • 54
  • 156
  • 201
41

You could do this:

find ./ -type f -ls |grep '10 Sep'

Example:

[root@pbx etc]# find /var/ -type f -ls | grep "Dec 24"
791235    4 -rw-r--r--   1 root     root           29 Dec 24 03:24 /var/lib/prelink/full
798227  288 -rw-r--r--   1 root     root       292323 Dec 24 23:53 /var/log/sa/sar24
797244  320 -rw-r--r--   1 root     root       321300 Dec 24 23:50 /var/log/sa/sa24
sjas
  • 18,644
  • 14
  • 87
  • 92
Jeff MacDonald
  • 588
  • 3
  • 2
  • This is what most people want, but it did not receive enough upvotes!!! – sjas Dec 31 '15 at 10:59
  • 3
    this might not work on having a few million files in one directory. – Dennis Nolte May 19 '16 at 15:17
  • 5
    There are side effects if the grep search term is in the file name. – Snorex Aug 24 '16 at 22:41
  • https://mywiki.wooledge.org/ParsingLs -- a better approach is probably to print the creation date in machine-readable form, perhaps with `stat` if you don't have `find -printf`. – tripleee Apr 05 '18 at 12:55
  • Regarding "million files" use `find ./ -type f -mtime -60 -ls | grep '10 Sep'` if this date is inside the last 60 days. – mgutt Jan 14 '22 at 08:10
14

You can't. The -c switch tells you when the permissions were last changed, -a tests the most recent access time, and -m tests the modification time. The filesystem used by most flavors of Linux (ext3) doesn't support a "creation time" record. Sorry!

Max Cantor
  • 8,229
  • 7
  • 45
  • 59
  • 8
    In fact, it's not just the filesystem type - there is no system interface for obtaining such information, even if the filesystem held it. One of the deficiencies of Unix going back to the earliest days, which is why Unix will never take off. – chrisdowney Jun 16 '11 at 11:41
  • 2
    Unix will never take off? – lucid_dreamer Oct 28 '19 at 20:58
8

@Max: is right about the creation time.

However, if you want to calculate the elapsed days argument for one of the -atime, -ctime, -mtime parameters, you can use the following expression

ELAPSED_DAYS=$(( ( $(date +%s) - $(date -d '2008-09-24' +%s) ) / 60 / 60 / 24 - 1 ))

Replace "2008-09-24" with whatever date you want and ELAPSED_DAYS will be set to the number of days between then and today. (Update: subtract one from the result to align with find's date rounding.)

So, to find any file modified on September 24th, 2008, the command would be:

find . -type f -mtime $(( ( $(date +%s) - $(date -d '2008-09-24' +%s) ) / 60 / 60 / 24 - 1 ))

This will work if your version of find doesn't support the -newerXY predicates mentioned in @Arve:'s answer.

yukondude
  • 24,013
  • 13
  • 49
  • 58
3

With the -atime, -ctime, and -mtime switches to find, you can get close to what you want to achieve.

ayaz
  • 10,406
  • 6
  • 33
  • 48
-4
cp `ls -ltr | grep 'Jun 14' | perl -wne 's/^.*\s+(\S+)$/$1/; print $1 . "\n";'` /some_destination_dir
Tintin
  • 1
  • 1
  • 2
    Why are you copying the files? Besides the command in `\`ls ...\`` works, it does not use the `find` tool. – Yamaneko Nov 08 '12 at 16:47
  • https://unix.stackexchange.com/questions/128985/why-not-parse-ls-and-what-to-do-instead – Danin May 07 '23 at 19:32
-6

I found this scriplet in a script that deletes all files older than 14 days:

CNT=0
for i in $(find -type f -ctime +14); do
  ((CNT = CNT + 1))
  echo -n "." >> $PROGRESS
  rm -f $i
done
echo deleted $CNT files, done at $(date "+%H:%M:%S") >> $LOG

I think a little additional "man find" and looking for the -ctime / -atime etc. parameters will help you here.

Georgi
  • 4,402
  • 4
  • 24
  • 20