Cron Job to auto delete folder older than 7 days in Centos - linux

I have a folder with large files in centos and how to delete the files older than 30 minutes.
Please suggest your ideas and snippets

It's simple. just use find and add the following line incrontab:
30 * * * * find /path/to/dir -type f -mmin +30 -exec rm -f {} \;
the above command will run every 30 minutes and delete ONLY files older that 30 minutes from the directory /path/to/dir

Related

CRON to Clear out /tmp files older than 30min old

the servers root /tmp directory has been getting full with file uploads. These files should be removed once the upload is done, but in some cases it hasn't. As i investigate that issue. Im looking for a cron task to delete files that start 202 from the /tmp directory that's 30 minutes old that runs every 30 minutes.
So far, i have /30 * * * rm -rf /tmp/202*
Found this solution. Works pretty well.
0 * * * * find /tmp -regextype posix-egrep -regex '/tmp/[0-9]{8}-[0-9]{6}-.*-file-.*' -type f -mmin +60 -delete

find and delete command deletes all files in that folder

Currently, I am using this command to delete all files older than 30 minutes on my Linux server.
sudo find /var/www/html/folder/* -type f -mmin +30 -delete
But it deletes all the files in that folder irrespective of their age. What's wrong with this?

Command line to remove oldest backup

I am having the following directory with multiple Backup from $(date +"%d.%m.%Y at %H:%M:%S").zip files.
/opt/
/opt/files/
/opt/files/private/*
/opt/files/backup.sh
/opt/files/backup.txt
/opt/files/Backup from $(date +"%d.%m.%Y at %H:%M:%S").zip
With a daily cronjob 0 0 * * * cd /opt/files/ && ./backup.sh > /opt/files/backup.txt I am currently managing my backups.
As you can imagine, this directory gets bigger and bigger over time. I now would like to create another script (or cronjob if it works with one command) to delete the oldest /opt/files/Backup from $(date +"%d.%m.%Y at %H:%M:%S").zip after 14 days (so that I have 14 recent backups all the time).
It would be great if you could explain your answer.
find /opt/files/Backup -name \*.zip -a -mtime +14 -ls
If you are satisfied the files being matched are the ones to delete, replace -ls with "-exec rm {} \;"

A crontab to move completed uploads from one dir to another?

I'm using the following crontab, once an hour, to move any files with the .mp3 extension from the dir "webupload" to the dir "complete" :
60 * * * * find usr/webupload -type f -maxdepth 1 -name "*.mp3" -exec mv {} usr/webupload/complete \;
The problem is that "webupload" contains lots of partial files being transferred.
I've read about a lot of different ways to achieve this but I think i'm more confused now than I was when I started!
What is the best practice or easiest way to only move the completed uploads?
Many thanks :)
It's going to be hard to tell when a file is completely written unless it is renamed when the download is completed, but you could change your find command and add -mmin +1 so that it only looks for files which have been modified more than 1 minutes ago (meaning that download is likely completed). Also, you should use / at the beginning of your paths rather than the relative paths your using:
60 * * * * find /usr/webupload -type f -mmin +1 -maxdepth 1 -name "*.mp3" -exec mv {} /usr/webupload/complete \;
You could obviously make the modification time longer (eg. 10 minutes -mmin +10) if you want to be more certain that the file has been downloaded.

deleting old files using crontab

I use the following crontab record in order to daily backup my DB:
0 2 * * * MYSQL_PWD=password mysqldump -u user db_name > $HOME/db_backups/db_name-$(date +\%Y-\%m-\%d-\%H-\%M).sql 2>> $HOME/db_backups/cron.log
I want to add another crontab record that will delete the DB dumps that are older then one month.
Any thoughts?
find /db_backups/ -mtime +30 -delete
This command would delete DB backups older than 30 days.
Just create another cron:
0 3 * * * find $HOME/db_backups -name "db_name*.sql" -mtime +30 -exec rm {} \; >> $HOME/db_backups/purge.log 2>&1
It will find all backups older than 30 days and delete them.
There is a tool called tmpreaper that securely deletes files matching certain criteria, such as an access or modification date n days in the past.

Resources