4

I read all the related questions and was unable to understand them. I am using Plesk CPanel to set cron job as it was advised by everyone.

I want to delete all files from a folder after 24 hours. Assume that I have to delete it after every 2 mins (So I can check its working or not).

I have two options:

  1. Either run a PHP file that deletes all files after 24 hours using a cron job
  2. Use the cron job command `rm` to delete all the files

I tried both ways and was unable to get my task completed.

Here is the pic of cpanel scheduled task:

http://i41.tinypic.com/2n0tsfs.png

I want to delete files from folder var/www/example.com/public/js/complied. All files inside this complied folder should be deleted. I don't know which to write in Command textfield.

Should I use the following command?

rm /var/www/example.com/public/js/compiled/*.*

Or should I execute a php file?

env php -q/var/www/example.com/public/js/cron.php

The source code of this Cron.php is:

<?php
$dir = "compiled"; // directory name



foreach (scandir($dir) as $item) {
    if ($item == '.' || $item == '..')
        continue;

        unlink($dir.DIRECTORY_SEPARATOR.$item);
        echo "All files deleted";
    }   
//rmdir($dir);

?>

I have tested this code and it works fine.

Thanks in advance.

Wes Crow
  • 2,971
  • 20
  • 24
user2290749
  • 53
  • 1
  • 3
  • 7

4 Answers4

10

I use this in a shell script...

find /some/path -mtime +7 -exec rm {} \; # delete > 7 days old
MrCleanX
  • 416
  • 2
  • 13
  • but he is trying -mtime +7 to delete files after 7 days how i can do it in every 2 mins coz i know for every 24 hour its should be -mtime +1 but what if i want to test it in every two minutes? – user2290749 May 01 '13 at 20:05
  • 1
    The command will delete files older than 7 days.. you can set the cron job to run every minute or however often you like, so that every minute you are deleting files that are older than 7 days (or 1 in your case). If you want to modify the command as well for testing, see the man page for find: http://linux.die.net/man/1/find – Jeff Lambert May 01 '13 at 20:36
  • how to exclude .php file? – Saurin Dashadia Feb 10 '18 at 13:23
  • @user2290749 -mmin 2 – MrPHP Jul 17 '18 at 10:19
8

To optimize MrCleanX' solution a bit, use xargs:

find /some/path -type f -mtime +7 -print0 | xargs -0 --no-run-if-empty rm

Instead of calling rm for each file to delete, xargs packs many files together to a single call to rm

The -print0 and -0 stuff is to make both find and xargs using NULL terminated strings, which is necessary to handle file names with space and other interesting chars in their names.

mogul
  • 4,441
  • 1
  • 18
  • 22
  • Note that if you don't include the `-print0`, xargs may just fail with a cryptic error `xargs: argument line too long`. – Moshe Katz Apr 08 '14 at 22:18
8

If you have access to your Server or SSH, you can simply add it to your crontab.

In your SSH just type

crontab -e

you will see a list of cron jobs on it, just append this line of code to your cronjob:

0 10 * * * rm -rf /var/www/example.com/public/js/compiled/*

The code above means that every 10am in the morning you are removing all the files in the path you provide. Please refer to this link for more info about Cron: http://en.wikipedia.org/wiki/Cron

JiNexus
  • 2,754
  • 1
  • 19
  • 17
6

it worked for me to delete in once a day

0 0 * * * rm -rf /home/user/public_html/folder

if you want to remove everything in this folder, but leave the folder itself:

0 0 * * * rm -f /home/user/public_html/folder/*
Shuhad zaman
  • 3,156
  • 32
  • 32