Find and tar files which are X hours old - shell

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

Related

How to find files older than 60 minutes only in current directory

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

Trying to remove a file and its parent directories

I've got a script that finds files within folders older than 30 days:
find /my/path/*/README.txt -mtime +30
that'll then produce a result such as
/my/path/jobs1/README.txt
/my/path/job2/README.txt
/my/path/job3/README.txt
Now the part I'm stuck at is I'd like to remove the folder + files that are older than 30 days.
find /my/path/*/README.txt -mtime +30 -exec rm -r {} \;
doesn't seem to work. It's only removing the readme.txt file
so ideally I'd like to just remove /job1, /job2, /job3 and any nested files
Can anyone point me in the right direction ?
This would be a safer way:
find /my/path/ -mindepth 2 -maxdepth 2 -type f -name 'README.txt' -mtime +30 -printf '%h\n' | xargs echo rm -r
Remove echo if you find it already correct after seeing the output.
With that you use printf '%h\n' to get the directory of the file, then use xargs to process it.
You can just run the following command in order to recursively remove directories modified more than 30 days ago.
find /my/path/ -type d -mtime +30 -exec rm -rf {} \;

Need to find files modified between 15 and 5 minutes ago

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

Recursively delete files in directory

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.

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