How to find file accessed/created just few minutes ago

129,322

Solution 1

Simply specify whether you want the time to be greater, smaller, or equal to the time you want, using, respectively:

find . -cmin +<time>
find . -cmin -<time>
find . -cmin  <time>

In your case, for example, the files with last edition in a maximum of 5 minutes, are given by:

find . -cmin -5

Solution 2

If you have GNU find you can also say

find . -newermt '1 minute ago'

The t options makes the reference "file" for newer become a reference date string of the sort that you could pass to GNU date -d, which understands complex date specifications like the one given above.

Solution 3

To find files accessed 1, 2, or 3 minutes ago use -3

find . -cmin -3

Solution 4

If you know the file is in your current directory, I would use:

ls -lt | head

This lists your most recently modified files and directories in order. In fact, I use it so much I have it aliased to 'lh'.

Share:
129,322
yuan
Author by

yuan

Updated on July 08, 2022

Comments

  • yuan
    yuan almost 2 years

    I always forget which file I edit one minutes ago, so I input find . -cmin 1 or some other value but it worked exactly 1 minutes. I had to try find . -ctime 2 /*or 3,4...*/.

    Then I find another approach which be better:

    touch -t 12251134 empty /*similar format which 5 or 10 minutes ago */
    find . -newer empty
    

    I can use date -d'-5minutes' +%m%d%H%M caculate the time for me. I want to know if there is a simple way to find files accessed 1, 2 or 3... minutes ago.

    • Icarus3
      Icarus3 over 11 years
      Why not to use "history" ? So, using arrow keys, you can look up for previous commands you used.
    • potrzebie
      potrzebie over 11 years
      I use ls -Ahl -crt | tail -5. So often that I made an alias for it. mc with modified-sorting would be even better.
  • yuan
    yuan over 11 years
    I wonder why they didn't include this helpful usage in the man page
  • Rubens
    Rubens over 11 years
    Yes, it is there, right in find man page, just look for: Numeric arguments can be specified as: +n, for greater than n; -n, for less than n; n, for exactly n. Them kids who wrote these unix programs did wonders (:
  • Stunner
    Stunner almost 6 years
    what if there are more than 10 files but you don't know the number. Then head doen't work
  • Neithan Max
    Neithan Max over 5 years
    head still works for that purpose, 10 lines is just the default. You can specify however many you want. First five lines? head -5. First twenty lines? head -20. Same with tail
  • ari gold
    ari gold over 5 years
    Eh, kiiiinda. That bit about how you can use + and - is, at least on my system, one level up in the hierarchy: under the "Primaries" heading. I was surprised that it was only matching exactly so I checked for another.. well "primary" that would help. Eventually good ol' SO came to the rescue :)
  • Johan Boulé
    Johan Boulé almost 5 years
    Just a note that if you want mtime, there's also the mmin option
  • sorpigal
    sorpigal over 4 years
    For this simple case -mmin would work, but you can also say -newermt 'yesterday' or -newermt '+ 2 weeks' or even -newermt '2PM last Sunday'. Calculating these things by hand could be done, but it's much easier to let find figure it out.