Hi,
my php script creates temp files in a folder. how do i set up a cron job that periodically deletes files older that 1 day.
im not sure about the command to delete files older than 1 day in Linux.
thanks for your help

reg
kevin

    you can get the modified time from [man]filemtime[/man], then unlink the ones that are older than 1 day using [man]date[/man] or whatever

      thanks for the reply stolzboy, i was thinking more in terms of a direct linux command.
      not using CRON to run a PHP script.

      reg
      keivn

        try this, it worked for me

        find /yourdir/tmp -mtime +1 -exec rm {} \; //where 1 is number of days since it was modified

          8 days later

          Thanks for the tip.. I was also looking for this.

          Best regards.

            3 years later

            I know this thread is like a billion years old, and I know f-all about cron or UNIX shell commands for the most part, but I'm assuming that

            find /yourdir/tmp -mtime +1 -exec rm {} \;

            won't work on directories, too.

            Is there a way to recursively delete all directories, subdirectories, and files in this fashion?

            Thanks for any help. It is MUCH appreciated.

              no, that doesn't work on directories, you can find the modified time of a directory tho, check out www.php.net/filemtime

              you can delete recursively with this

              function mostRecentModifiedFileTime($dirName,$doRecursive) {
                 $d = dir($dirName);
                 $lastModified = 0;
                 while($entry = $d->read()) {
                     if ($entry != "." && $entry != "..") {
                         if (!is_dir($dirName."/".$entry)) {
                             $currentModified = filemtime($dirName."/".$entry);
                         } else if ($doRecursive && is_dir($dirName."/".$entry)) {
                             $currentModified = mostRecentModifiedFileTime($dirName."/".$entry,true);
                         }
                         if ($currentModified > $lastModified){
                             $lastModified = $currentModified;
                         }
                     }
                 }
                 $d->close();
                 return $lastModified;
              }
              
                16 days later

                Cool, thanks for the reply!

                I guess my only outstanding question would be, "How can I run such a script automatically and at a specified interval?"

                What's the best-practice/standard way to do this? Use cron to execute the script using the command-line version of PHP?

                Thanks again =)

                  a month later

                  find /yourdir/tmp -mtime +1 -exec rm {} \; //where 1 is number of days since it was modified

                  Is there a way to make it delete a specific file in the directory every hour? I would rather not run a php script to do this.

                  Lets say I wanted to delete
                  /yourdir/tmp/temp.txt every hour. Is there a way to do it?

                    a month later

                    Hi, You can run a php script as a shell script to delete files(dirs) every hours!

                    Here is a part of my script:

                    
                    #!/usr/local/php511/bin/php
                    <?php
                    function your_functions()
                    	// check and delete files
                    }
                    
                    function run_self()     {
                            your_functions();
                            sleep(3600);
                            run_self();
                    }
                    
                    run_self();
                    
                    ?>
                    
                    

                    Save the script as a shell script like : delete_tmp_files.sh

                    chmod +x delete_tmp_files.sh

                    Run it:
                    ./delete_tmp_files.sh &

                    It works for me, and only use a little CPU resource!

                      22 days later
                      stolzyboy wrote:

                      find /yourdir/tmp -mtime +1 -exec rm {} \; //where 1 is number of days since it was modified

                      Excellent. Almost what I needed. Can I alter this command so that it doesn'n delete index.html ?

                        3 months later

                        Hello,

                        I'm totally new to PHP and don't know what I'm doing wrong!

                        Tried running:
                        find /yourdir/tmp -mtime +1 -exec rm {} \;
                        in a php file but I am getting an error.

                        I made an text file and named it dtf.php and dropped it in an directory called dtf. When I executed the file I want that it deletes files in the directory tmp which is located in de directory jcms. Doing this I get an Parse Error.

                        <?php
                        find /jcms/tmp -mtime +1 -exec rm {} \;
                        ?>

                        Bert

                          Write a Reply...