find and number of days range - bash

I tried to code in bash an archiving script but I can't seem to get find() working with an interval in number of days.
The ranges I need to code are
files last modified between today and 31 days old. This works:
find . -name "*.VER" -mtime -31 -exec mv '{}' /opt/html/31';' -print
files last modified between 31 days and 62 days old. This does not work:
find . -name "*.VER" -mtime -31 -mtime -62 -exec mv '{}' /opt/html/62 ';' -print
files last modified between 62 days and 93 days old
files last modified between 93 days and 124 days old
...you get the idea (up to year)....
Is there a way to code my find() command to use a number of days range??

I think you have to change the logic of + and - in the times:
find . -name "*.VER" -mtime +31 -mtime -62 -exec mv '{}' /opt/html/62 ';' -print
This tells: files with a mtime greater than 31 days but less than 61 days.

Related

Move all the Files generated 23 hrs. after they were first created

I am using below command to pick the files 23 hrs after they were first created, but it is not picking can you tell me where i am going wrong
find /test/files -maxdepth 1 -type f -mtime +0.9
mtime +1 means 24 hours later
so, used +0.9 so it should pick 23 hours , but it is not picking.
I am afraid fractions will not work in find. What you can do is to create file with timestamp 23 hours ago and with find get older files:
touch -d '23 hours ago' /tmp/tmp_file
find /test/files -maxdepth 1 -type f ! -newer /tmp/tmp_file

Find and tar files which are X hours old

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

Find files created 1 hour ago

I want to list files with long description on AIX that was created from 1 hour ago. I am trying
find . -cmin -60 but it only shows the file names. Was trying also find . -cmin -60 -exec ls -l {} \; but it was displaying the whole files in the directory.
Thank you
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.
Some options:
find . -cmin -60 -exec ls -ld {} \;
find . -cmin -60 -type f -exec ls -l {} \;
find . -cmin -60 -print0 | xargs -0 ls -ld
find . -cmin -60 -type f -print0 | xargs -0 ls -l
The last two are better, but require GNU-findutils.
Edit: As noted by others, -cmin -60 means recently modified files, -cmin +60 means the not-recently modified files.
When your find lacks a cmin option, you can touch a file with a timestamp 1 hour ago and use the find with -newer.

In unix, how to extract last 60 days files from yesterday

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.

find -mtime files older than 1 hour [duplicate]

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.

Resources