Archiving old files after 18 months


If you find this free website useful – why don’t you support this with a donation? It is easy…. read more ….

In a WordPress installation I have an uploads folder that contains html files, that need to be archived away after 18 months. As the links are dynamically built I don’t need to worry about anything except moving them. If they were normal WordPress uploads that would be different.

So in this case a simple monthly cron job using find suffices

cd /home/mysite/wp-content/uploads/mydir; /bin/find . -not \( -path ./archive -prune \) -mtime +547 -name '*.html' -exec mv "{}" ./archive \;

Explained

cd /home/mysite/wp-content/uploads/mydir; Switch to the working directory – why not just specify this in the find command? You could, but it is safer to change directory as a mistake could end up archiving stuff you didn’t mean to.

/bin/find as it is a cron job, the full path of the find command

. find in the current directory and downwards

-not ( -path ./archive -prune ) exclude the directory called archive as that is in the working directory

-mtime +547 anything over 547 days ( 18 months )

-name ‘*.html’ only files ending html in this case

-exec mv “{}” ./archive \; execute the command on each file move filename to archive


Leave a Reply

Your email address will not be published.