Can you create a cron to delete files with retroactive dates?

Asked

Viewed 789 times

1

See my need, I have a cron that runs every morning and creates a file.ddmmaaaa.backup on the Linux Kubuntu server, it happens that the hard drive fills up quickly since this backup file has 500MB on average, how to create a cron that deletes files a date earlier than a week from the current date?

I kept thinking about how to do this but I don’t know much about cron and how I could put variables in cron, maybe the solution is script, but rs, I’m also very layy in script.

The Exclude Script would be roughly

delete /folder/file*. backup

Where file* would be with earlier dates

  • I was browsing the Stack questions and I came up with a solution, I will create a php script with my needs and run in cron

  • 1

    Exactly, cron is only a utility that performs scheduled tasks. Nothing is programmed in it beyond the periodicity of the tasks that must be performed.

  • I was browsing the Stack questions and I came up with a solution, I will create a php script with my needs and run in cron

  • what script @Marcelo ???

  • Yes a php script since this server has an apache and php running :-)

1 answer

1

You can use the command find to fetch the old files and then remove them.

find /path/to -type f -mtime +7 -print0 | xargs -0 rm --

This way, the command will list all files that were modified at least 7 days ago.

You can override the parameter -mtime for -atime or -ctime, which checks the date of the last time the file was accessed or changed, respectively.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.