3

If i have 3 files called 1.txt,2.txt and 3.txt for example and they were created an hour apart, say 1pm 2pm and 3pm respectively. What I need is a command that finds all files modified within an hour of a specific file.

I'm in the same directory as the files in the terminal and all files are setuid permission

I've been trying:

find . -type f -perm -4000 -newer 2.txt -mmin -60 -print

This should return 3.txt but it doesn't

What would use to file created in the hour before or after 2.txt?

Nexus490
  • 319
  • 1
  • 4
  • 14

4 Answers4

2

Try this

touch /tmp/temp -t time1

touch /tmp/ntemp -t time2

find . -newer /tmp/temp -a ! -newer /tmp/ntemp -exec ls -l {} \; 2>/dev/null

where

time1 = time of file creation - 1hr

time2 = time of file creation + 1hr

Ex:

time1 = 201210041500

time2 = 201210041700

Odin
  • 114
  • 1
  • 4
1

Here is my logic -

First get time of last access of file in seconds since Epoch in some variable.

time_in_sec=$(stat -c %X 2.txt)

Get the time of last hour ( ie 3600 seconds back )

one_hr_old_time_in_sec=`expr $time_in_sec - 3600`

Convert it into format suitable for find command

newerthan=$(date -d @$one_hr_old_time_in_sec '+%m-%d-%y%n %H:%M:%S')

Convert time of original file in format suitable for find command

olderthan=$(date -d @$time_in_sec '+%m-%d-%y%n%H:%M:%S')

Get the list of files modified between two time periods using find command

find . -newermt "$newerthan" ! -newermt "$olderthan" -print

If it works you can write a small shell script which will take file name as parameter and can try for +3600 also.
Honestly, I haven't tried it. Hope it works !

Vikram
  • 1,999
  • 3
  • 23
  • 35
1

The previous solution can be simplified since -newermt shall accept the same formats than 'date' including @N for N seconds since epoch.

Also for the 'stat' command it is probably better to use -Y (the time of last modification) instead of -X (the time of last access).

So the command to find the files modified + or - 1 hours of 2.txt is

N=$(stat -c %Y 2.txt)  
find . -newermt @$((N-3600)) ! -newermt @$((N+3600)) -print
MarmiK
  • 5,639
  • 6
  • 40
  • 49
0

If you are running this after 4pm, given your example, it makes sense that it wouldn't return 3.txt, as -newer 2.txt -mmin -60 means "modified in the last 60 minutes, and more recently than 2.txt", not "modified less than 60 minutes after 2.txt". I don't think find currently has options to do what you're wanting (at least, the version I have doesn't), but it shouldn't be too hard to script in python or perl.

twalberg
  • 59,951
  • 11
  • 89
  • 84