I'm trying to find all files in a given folder that were modified withing a certain time frame, say between 5 and 15 minutes ago.
Currently I can find anything modified say up to 15 minutes ago by using find -cmin
#!/bin/bash
minutes="15"
FILETYPES=`find . *PATTERN*.txt* -maxdepth 0 -type f -cmin -$minutes`
How do I give it a time frame?
Try this :
find . -name '*pattern.txt' -maxdepth 1 -type f \( -mmin -15 -a -mmin +5 \)
Notes
the parenthesis are not mandatory here with and : -a, but it's necessary for case with or: -o
always use single quotes around the pattern to prevent shell expansion of the wildcard
to give a pattern, use -name or -iname
for the date/hour, -mmin is the way to go for minutes and -mtime for days.
Using find, you can add additional conditions to create the range. Each condition is implied as "and" unless -o is used. You also want -mmin instead of -cmin for modified time (but they are often the same).
find . '*PATTERN*.txt*' -maxdepth 0 -type f -mmin -15 -mmin +5
Related
I am trying to tar all the log files of a specific folder which are X hours old, i have done it with X days and I need it for X hours.
find $DEST_DIRECTORY/*.log -type f ! -name "*.tar.gz" -mtime +$hours -exec mv '{}' ${DEST_DIRECTORY}/${TAR_DIR_NAME}/ \
Above code is not working for hours.
Use:
-mmin n to filter for files that were modified n minutes ago.
So in your example you should replace -mtime +$hours with -mmin +$[$hours * 60].
Full docs are here: http://man7.org/linux/man-pages/man1/find.1.html
you should try this :
find $DEST_DIRECTORY/*.log -type f ! -name "*.tar.gz" -mmin -180
180 refers to 3 hours and you can tweak it to your need
I'm able to extract last 60 days files from current date. But I want last 60 days files from yesterday.
Below is the command, I'm using to fetch last 60 days files
find . -name $val\* -mtime -60 -print
I can pipe the output to another find probably like below
find . -name $val\* -mtime -60|find . -name $val\* -mtime 1 -print
But that would produce files which was modified exactly one days ago from the list of last 60 days files.
Pls help me how to achieve it.
You can provide multiple predicates to a single find command to filter the
list of files being returned. In this case, combining -mtime +1 with your
first command will return all the files that have been modified less than 60 days ago and (logical AND is implicit) more than one day ago:
find . -name $val\* -mtime -60 -mtime +1 -print
Is this command useful?
find . -name $val\* \( -mtime -61 -and -not -mtime -1 \) -print
It will list all last 61 days files but exclude files which were modified in last 24 hours.
The following command it finding all the files which are older than 60 minutes but in sub directories also.
find . -type f -mmin +60 -print
How can we restrict it find files only in given directory?
I have archive folders in sub direcotries which have older files that is causing problem.
Thanks in advance :)
Use the -maxdepth 1 argument to find to limit results to the current directory.
So your full command would be find . -type f -mmin +60 -maxdepth 1 -print
find ./your-directory -daystart -maxdepth 1 -mmin +10 -type f -delete
How do I recursively delete files in directory that were modified more than 6 hours ago?
This example work for 1 day:
find /data2/input -type f -mtime +1 -delete -print
Use -mmin instead of mtime. It will allow you to specify the number of minutes since the files was last modified. So for files older than 6 hours:
find /data2/input -type f -mmin +360 -delete -print
Check the flags -cmin or -mmin in the manual page.
This question already has answers here:
How to delete files older than X hours
(9 answers)
Closed 6 years ago.
I have this command that I run every 24 hours currently.
find /var/www/html/audio -daystart -maxdepth 1 -mtime +1 -type f -name "*.mp3" -exec rm -f {} \;
I would like to run it every 1 hour and delete files that are older than 1 hour. Is this correct:
find /var/www/html/audio -daystart -maxdepth 1 -mtime **+0.04** -type f -name "*.mp3" -exec rm -f {} \;
I am not sure of my use of the decimal number??
Thanks for any corrections.
EDIT
OR could I just use -mmin 60? Is this correct?
EDIT2
I tried your test, good thing you suggested it. I got an empty result. I want all files OLDER than 60mins to be deleted! How can I do this?? Does my command actually do this?
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.