9

If I want to find a file by name, I do the following:

find -name app

If I want to find a file by type, I do the following:

find -name app -type d

However, because app is such a generic name, many results show up. I would like to find a directory named app, which was created today. Is there a flag or command to achieve this?

Eliah Kagan
  • 117,780
JohnMerlino
  • 7,309
  • 1
    *nix file systems don't record the file creation/birth time and GNU find(1) doesn't offer to search by birth time. That's why all answers are and will be about (status) modification time. – David Foerster Oct 05 '14 at 13:10
  • @DavidFoerster: so what does the -ctime switch do then? – Thomas Weller Oct 18 '15 at 17:31
  • 1
    @ThomasWeller: The manual says, “-ctime n: File's status was last changed […]” (emphasis added). Nothing about file creation time. – David Foerster Oct 18 '15 at 17:48
  • @DavidFoerster: thanks. So we have -mtime for modified and -ctime for changed. That sounds really great and absolutely not confusing. Well, not your fault... – Thomas Weller Oct 18 '15 at 17:53
  • @ThomasWeller: Yeah, having -smtime or something alike for file *status modification time* would be more intuitive. – David Foerster Oct 18 '15 at 17:54

4 Answers4

9

To search the whole filesystem (/) for

  • directories (-type d),
  • called app (-name app),
  • that were modified more recently than one day (i.e., 24 hours) ago (-mtime 0),

...Use:

find / -name app -type d -mtime 0

Source: man find.

In particular see the explanation of the -mtime flag and the "find $HOME -mtime 0" example:

[T]o match -mtime 0, a file will have to have a modification in the past which is less than 24 hours ago.

(Thus the time in days since the file was modified is rounded down to the nearest integer for purposes of being matched by -mtime.)


Creation is considered a form of modification for the purpose of file timestamps, so this will work even if the file's contents weren't altered after it was created. It will also match folders with modification timestamps in the last day that were created earlier, but you probably don't have many such folders whose exact name is app.

When a file was created is not typically stored in the filesystem. But the time at which its metadata were last changed (e.g., name/location, ownership, permissions) is stored. If you prefer to go by that to when the file's contents were modified, use -ctime in place of -mtime:

find / -name app -type d -ctime 0

For both -mtime and -ctime, the original creation of the file qualifies as a modification / status change.

Eliah Kagan
  • 117,780
  • I also came across this article too: https://www.digitalocean.com/community/tutorials/how-to-use-find-and-locate-to-search-for-files-on-a-linux-vps – JohnMerlino Oct 05 '14 at 01:25
4

Get all modified file between the date: 2020-02-28 to 2020-03-28

find . -type f -newermt 2020-02-28 -not -newermt 2020-03-28
Benny
  • 4,920
3

Command find options:

If you need a specific date range many days ago.

Example find files modified between Feb/1/2013 and Feb/1/2014, in /data/docs directory:

touch --date "2013-02-01" /tmp/start
touch --date "2014-02-01" /tmp/end
find /data/docs -type f -newer /tmp/start -not -newer /tmp/end

You can save list to a text file called findlist.txt as follows:

find /data/docs -type f -newer /tmp/start -not -newer /tmp/end > findlist.txt
kyodake
  • 15,401
  • Hard to believe that I need 5 lines (your 3 + 2 for deleting the temp files) to find files in a certain date range. – Thomas Weller Oct 18 '15 at 17:28
  • @ThomasWeller note that it is good to know that this indicates one can filter listed files based on other file dates. – david Jun 28 '22 at 10:07
-1

You can use the modification time, either

find -name app -type d -mtime -1

(modified less that 1 x 24 hours ago) or

find -name app -type d -newermt yesterday
steeldriver
  • 136,215
  • 21
  • 243
  • 336