Skip to content
Advertisement

How to find all the files created today and modified anytime before 5 minutes?

Let’s say it is 10:00 am when the command is run, I need to list the files modified between midnight and 9:55 am.

I have tried the following command which looks logical as per the documentation but it is pulling all the files since yesterday.

-rw-r----- 1 anami anami 0 Jan 26 17:51 a
-rw-r----- 1 anami anami 0 Jan 26 18:18 b
-rw-r----- 1 anami anami 0 Jan 26 01:00 c
-rw-r----- 1 anami anami 0 Jan 25 10:00 d
-rw-r----- 1 anami anami 0 Jan 27 12:21 e
-rw-r----- 1 anami anami 0 Jan 27 12:52 f


$ find . -daystart -mmin +5

./a
./g
./d
./c
./e
./b
./f

Advertisement

Answer

Since -daystart isn’t that portible, consider a solution like so;

  1. Use $(date +"%H") to get the current hour, in your test case, this should be 10.

  2. Multiply that number by 60 to get the desired minutes

  3. Subtract 5 minutes to get the number of minutes between 00:00 and 09:55

  4. Use that value for the -mmin param

currentHour=$(date +"%H")
minutes=$(( 60 * currentHour - 5 ))
find . -mmin "-${minutes}" -mmin +5
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement