TOPIC OF ARTICLE BELOW: LINUX – find files older or younger then certain days – delete them too if you want – mtime
Similar articles:
Find & Delete files between 2 dates
Find & Delete files young and older then X days (same topic as this article)
THE ARTICLE:
mtime on find
Lets say you have a folder /backup/logs/ and it has 30 logs files, one for each day. And you want to keep the 7 newest log files. Delete the rest: How to select (I have another article where you select by making files with creation date attribute changed, and using find to find files in between the dates of the 2 files – just search infotinks for find) using -mtime option:
#find [options] -mtime n
#find [options] -mtime -n #find [options] -mtime +n Where n is number of days
Did you go through the man page clearly?
+n – for greater than n, -n – for less than n, n – for exactly n. If you have hidden files to look thru:
shopt -s dotglob
Find files newer then 7 days (not including 7th day, for seventh day its no sign not -7 and not +7 just 7):
find /backup/logs/* -type f -maxdepth 0 -mtime -7
Delete the older files (notice changed sign from – to +) (not going to delete 7th day, for seventh day its no sign not -7 and not +7 just 7) :
find /backup/logs/* -type f -maxdepth 0 -mtime +7 -exec rm -f {} \;
Remove shopt dotglob:
shopt -u dotglob
NOTE: maxdepth is 0 so it doesnt look for files inside the folders
Find has -and and -or operator, -and make sure 2 conditions are true, -or just either condition (useful to combine results of 2 different finds). I say 2 but can be more then 2.
How to select files younger then 3 days and including 3rd day:
find /whereever -type f -mtime -3 -or -mtime 3
So if -mtime -3 gave me 3 files and -mtime 3 gave me 1 file, both with an or would give me 4 files (3+1). If i used -and, then Id get no files because both conditions cant “and” up they are both never true for any of the files.
How compress only files 14 days old and younger:
find / -type f -mtime -14 | xargs tar -rf weekly_incremental.tar gzip weekly_incremental.tar NOTE: r for append to tar, f for file to save to. NOTE: dont compress at the same time with c, c doesnt work well with r Find files between 3 days and older and 6 days and younger:
We essentially need:
Find all files (olderthen 3 days OR 3 days old) AND (younger then 6 days OR 6 days old)
find /backup/logs/* -maxdepth 0 \( -mtime +3 -or -mtime 3 \) -and \( -mtime -6 -or -mtime 6 \)
NOTE: with find you can use parenthesis to change order of operations to help your logical finds and do alot more, just make sure to escape each parenthesis so ( becomes \( and ) becomes \). Escaping means using the \ key so bash doesnt do what it usually does with the character, and passes it on to find. In bash () are used to construct arrays and complex scripts but we need it for find so we escape it.
Fractional 24-hour periods are truncated! That means that “find -mtime +1” says to match files modified two or more days ago. find . -mtime +0 # find files modified greater than 24 hours ago The following may only work on GNU version of find?
|