How to find a file that's modified more than 2 days ago but was modified less than 5 days ago by use of any Linux utility ?
Hello abdulbadii,
Following simple find
command may help you on same.(Though I haven't tested it)
find -type f -mtime +2 -mtime -5
Or in case you want to list them out following may help you on same:
find -type f -mtime +2 -mtime -5 -exec ls -lhtr {} \+
Thanks,
R. Singh
Please be aware that the results you get from above are depending on the point of time you run it, as the "n*24 hours ago" period starts exactly then. Run at 14:33h with -mtime +2
, a file modified two days ago at 14:30h will show up, and one from 14:35h will not. Run at 14:35h, both files will show up.
Does your find
version offer the -daystart
option? If not, you might need to adapt the -mmin
test...
If your find
doesn't have the -mmin
or -daystart
primaries (neither of which are specified by the standards), you can also use touch
to create two files with the timestamp boundaries you want at each end of your window and use:
trap 'rm -f /tmp/start_time_file.$$ /tmp/end_time_file.$$' 0
# Note that although the following uses midnight as the start and stop times,
# using touch allows you to set any time range you want. If you use -d instead
# -t to set timestamps, you can set ranges down to the nanosecond level if the
# filesystems on which the time range files and the file hierarchy being searched
# support timestamps to that level of detail.
touch -t 201802050000 /tmp/start_time_file.$$
touch -t 201802080000 /tmp/end_time_file.$$
find /directory/at/root/of/hierarchy -type f -newer /tmp/start_time_file.$$ ! -newer /tmp/end_time_file.$$
Note: Fixed to consistently use timestamp files created in /tmp and switch end and start files in the find
command as noted by RudiC in private mail.
I've rewritten all my code to use touch / find method Don mentioned here, some time ago.
I do remember having more hair back then, so we are talking years
Specifying ranges in such fashion (touch/find) will make your code work on any unix/linux operating system in existence without change.
Regards
Peasant.