Task: find and do something with files that were modified longer ago or earlier than a specified number of days.
For example, we need to delete all files older than 30 days. Or we need to copy all files that were modified no more than 30 days ago.
For this we'll use the find utility.
Find files older than N days:
$ find . -mtime +N
where instead of N specify the number of days, for example:
$ find . -mtime +30
Find files younger than N days:
$ find . -mtime -N
as an example:
$ find . -mtime -45
In these examples we're working with the current directory, indicated by the "." dot.
Delete files older than 30 days
$ find . -mtime +30 -exec rm {} \;
Delete files younger than 30 days
$ find . -mtime -30 -exec rm {} \;
Copy files older/younger than 30 days
$ find . -mtime +30 -exec cp {} /target/dir/ \;
where instead of /target/dir/ specify the directory to copy into.
Move files older/younger than 30 days
$ find . -mtime +30 -exec mv {} /target/dir/ \;
where instead of /target/dir/ specify the directory to copy into.
And everything else works the same way.
Comments