List of Files Modified 1 Hour Before

Find files modified over 1 hour ago but less than 3 days

Find has -mtime and -mmin:

find . -mtime +3 -mmin -60

From the find manual:

Numeric arguments can be specified as:

+n for greater than n

-n for less than n

n for exactly n

Find files created 1 hour ago

I think what you want is

find . -cmin +60 -exec ls -al {} \;

It will list all the files in current directory created more than 60 minutes agp.

The '+' in the '+60' means more than 60 minutes ago while a '-' in the '-60' means less than 60 minutes ago.

find files modified within given time range

-newermt primary doesn't accept a time range in any format. To select files modified within let's say the period A-B, you need two -newermts; one to include files newer than A, the other to exclude files newer than B.

Further, there are two edge cases that need to be dealt with:

  1. The user might enter 08 or 09 as both are valid hours. But as both have a leading zero, Bash would treat them as octal numbers in an arithmetic context, and raise an error since 8 and 9 are not valid digits in base 8.
  2. When the user entered 0, to include files modified at 00:00 too, inclusive -newermt's argument has to be yesterday's 23:59:59.

So, I would do it like this instead:

#!/bin/bash -
LC_COLLATE=C
read -rp 'hour ([0]0-23): ' hour
case $hour in
(0|00)
find /home/mikepnrs \
-newermt 'yesterday 23:59:59' \
! -newermt '00:59:59' ;;
(0[1-9]|1[0-9]|2[0-3])
find /home/mikepnrs \
-newermt "$((10#$hour-1)):59:59" \
! -newermt "$hour:59:59" ;;
(*)
printf 'invalid hour: %q\n' "$hour" >&2
exit 1
esac

Find the files that have been changed in last 24 hours

To find all files modified in the last 24 hours (last full day) in a particular specific directory and its sub-directories:

find /directory_path -mtime -1 -ls

Should be to your liking

The - before 1 is important - it means anything changed one day or less ago.
A + before 1 would instead mean anything changed at least one day ago, while having nothing before the 1 would have meant it was changed exacted one day ago, no more, no less.

find -mtime files older than 1 hour

What about -mmin?

find /var/www/html/audio -daystart -maxdepth 1 -mmin +59 -type f -name "*.mp3" \
-exec rm -f {} \;

From man find:


-mmin n
File's data was last modified n minutes ago.

Also, make sure to test this first!


... -exec echo rm -f '{}' \;
^^^^ Add the 'echo' so you just see the commands that are going to get
run instead of actual trying them first.


Related Topics



Leave a reply



Submit