Cron job to delete files created before a specific time
If you're using GNU find
, try this:
find /home/username/public_html/temp -type f -mmin +5 -delete
Should also work with FreeBSD or many other versions of find
.
Cron Job to auto delete folder older than 7 days Linux
For example, the description of crontab for deleting files older than 7 days under the /path/to/backup/
every day at 4:02 AM is as follows.
02 4 * * * find /path/to/backup/* -mtime +7 -exec rm {} \;
Please make sure before executing rm
whether targets are intended files. You can check the targets by specifying -ls
as the argument of find
.
find /path/to/backup/* -mtime +7 -ls
mtime
means the last modification timestamp and the results of find may not be the expected file depending on the backup method.
How to delete folders that are older than a day? (Cron Job)
This should do it:
find /path/to/dir -maxdepth 0 -ctime +1 -exec rm -fr {} +
But be careful, and test it first outside of cron
, without the -exec
part, so you don't delete something else by accident.
Delete folder older than 30 minutes with Cron
I actually found out that the best method is to divide the commands in Cron in 2 parts, and use the -delete
argument
Code
30 * * * * sudo find /my/folder/* -type f -mmin +30 -delete && sudo find /my/folder/* -type d -empty -mmin +30 -delete
Explanations
30 * * * *
: execute every 30mn all the timesudo find /my/folder/* -type f -mmin +45 -delete
: delete all files and subfiles that are older than 45 minutes&&
: do only if first command has successfully runsudo find /my/folder/* -type d -empty -mmin +45 -delete
: delete all empty folders that are older than 45 minutes
Working on Ubuntu 16.04
Auto delete files every one hour in ubuntu
Finally found a solution for delete files every one-hour using crontab
.
First, write the shell script.
root@admin:/home/ubuntu#vi delete_cache.sh
after creating the file add the below scripts in delete_cache.sh
rm -rf /var/tmp/*
then run a crontab
.
root@admin:/home/ubuntu# crontab -e
0 * * * * /bin/sh /home/ubuntu/delete_cache.sh
The 0 at the beginning means to run at the 0th minute. the script would run every hour. for more see here, you can create your own cronjobs
How to delete files from directory on specific time, daily?
The script you posted deletes .txt files in the given folder if they are older than a day but the problem is that this test only happens once - when you run the script.
What you need to do is run this script periodically. If you are running this on Linux you should probably add a cron job that executes this script periodically, say once an hour or once daily.
If you're running this on Windows, there is a task schedule that you could use to accomplish the same thing.
Related Topics
How to Pass a PHP Variable Using the Url
Laravel How to Return Single Column Value Using Query Builder
How to Prevent Mails Sent Through PHP Mail() from Going to Spam
Creating Advanced Search Query With PHP Mysqli
How to Get the Path to the Laravel Storage Folder
Unset Post Variables After Form Submission
Codeigniter - Call to a Member Function Result() on Boolean In
PHP Code Is Not Being Executed, But the Code Shows in the Browser Source Code
MySQLi or Pdo - What Are the Pros and Cons
How to Get Time Difference in Minutes in PHP
Sum Value in Foreach Loop Every 3 Time Looping
Validating Base64 Encoded Images
Php Fatal Error: Uncaught Pdoexception: Could Not Find Driver
Success Message Pop-Up After from Submit Button Clicked
Laravel 5.6 Class App\Http\Controllers\Postcontroller Does Not Exist
How to Set the Default Value of an Attribute on a Laravel Model