Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2003
    Location
    Proud She-Geek
    Posts
    1,722

    How difficult would it be...

    to create a simple script to scan my server and remove certain files (the types specified by me) if they are older than "x" number of days (also specified by me)?

    Does something like this already exist? Any help is appreciated!
    <?php echo "Signature here"; ?>

  2. #2
    Join Date
    Jul 2002
    Location
    Israel
    Posts
    351
    # export X=2
    # find / -type f -ctime $X -print

    or, to delete them,

    # find / -type f -ctime $X -exec rm -f {} \;
    C#/C++/TCL/Python/PHP developer.
    mark at mark org il

  3. #3
    Join Date
    May 2004
    Location
    Lansing, MI, USA
    Posts
    1,548
    Though you may want to use -mtime or -atime depending on if you care when it was Created, Modified, or Accessed (provided your server tracks atime, lots of people like to disable it to help on harddrive longevity
    Jacob - WebOnce Technologies - 30 Day 100% Satisfaction Guarantee - Over 5 Years Going Strong!
    Website Hosting, PHP4&5, RoR, MySQL 5.0, Reseller Hosting, Development, and Designs
    Powered By JAM - Professional Website Development - PHP, MySQL, JavaScript, AJAX - Projects Small & Large

  4. #4
    Join Date
    Jun 2003
    Location
    Proud She-Geek
    Posts
    1,722
    Thanks, MarkIL and WebOnce...

    ..just one question; with what extension would I save the code (or do I simply run the commands via SSH)? Also, how can I restrict the search to a certain directory? And finally, where would I put the file type variables?

    Thanks again!
    <?php echo "Signature here"; ?>

  5. #5
    Originally posted by P-nut
    with what extension would I save the code (or do I simply run the commands via SSH)?
    hi -

    you could just run the commands asuggested by MarkIL via SSH, assuming your default shell is something like Bash or Ksh. (If you're on a linux server then most likely you'll be at a Bash prompt.) If the shell is csh or tcsh (more common default on BSD and Solaris systems) then just replace the first line ("export X=2") with ("setenv X 2"). Or you could just skip that first line and plug whatever value you need directly into the "-ctime" parameter to the "find" command ...

    (refering to suggestion posted by MarkIL)
    Code:
    # export X=2
    # find / -type f -ctime $X -print
    Also, how can I restrict the search to a certain directory? And finally, where would I put the file type variables?
    To start the search in a particular directory, replace the '/' in the "find" command with the directory path you want. To limit the search to just the first level of that directory (prevent descending into sub-directories) add the "'-prune" flag.

    For your intended use, the file type as specified by the "-type" parameter should probably be left as "f" (regular file). I'm guessing that you might want to use filename extensions (ie, ".jpg") to select files - this could be done using the "-name" parameter.

    for example:
    Code:
     find /home/someuser/uploads -prune -type f -ctime 3 -name "*.jpg" -exec rm -f {} \;
    would delete all ".jpg" files that have not been modified in the last 3 days from someuser's uploads directory (but not any subdirectories).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •