Results 1 to 6 of 6
  1. #1
    ABW Ambassador
    Join Date
    January 18th, 2005
    Location
    Singapore
    Posts
    1,597
    Help. How to delete files before a certain date in Linux?
    I need to delete old files in a linux system.

    rm -f *.htm would just remove everything that ends with .htm

    How do I get it to delete everything dated before 15 Oct 2005?

  2. #2
    Fear and Arrogance jrrl's Avatar
    Join Date
    January 18th, 2005
    Location
    Pittsburgh
    Posts
    485
    Quote Originally Posted by womanht
    I need to delete old files in a linux system.

    rm -f *.htm would just remove everything that ends with .htm

    How do I get it to delete everything dated before 15 Oct 2005?
    Okay, please Please PLEASE, back everything up before you do this. Just in case. Unix/Linux gives you plenty of rope to hang yourself and I'd hate to have helped in that endeavor. That said, here's what you do...

    The command you want is 'find'. It is a pretty powerful command with more options than you can shake a stick at. For your job, you want it to look something like this:

    find . -type f -mtime XXX -maxdepth 1 -exec rm {} \;

    The '.' tells it to work in the current directory

    The '-type f' tells it to only look at files, not links or directories

    The '-maxdepth 1' tells it to only work in the current directory, rather than diving into subdirectories

    The '-mtime XXX' tell it to only match files that were last modified XXX*24 hours ago, so use whatever number you want in place of XXX

    The '-exec rm {} \;' tells it to remove any files that have matched all the other conditions. You can put pretty much any command after '-exec'. The curly braces will be replaced by the file name it is looking at. Command in '-exec' have to be terminated by '\;'.

    PLEASE backup first! find is a very cool command once you get it, but you can do very bad things with it if you make mistakes.

    If I am deleting things, I usually use '-print' instead of the '-exec blahblah' first to just get a list of files it is matching. Then, if it is finding the right ones, I do it again with the -exec clause.

    Good luck!

    -John.
    There's a reason army's wear uniforms even though it makes them easier to spot. Sometimes that's what you want. Uniforms suggest organization, power, and numbers. These, in turn, inspire fear. And, as any good operative knows, there is no more effective weapon than fear.

    Hosting Comparison - HostScope - jrrl.com

  3. #3
    ABW Ambassador
    Join Date
    January 18th, 2005
    Location
    Singapore
    Posts
    1,597
    Thanks. Will try that, very carefully on a place that is backed up first.

  4. #4
    Moderator MichaelColey's Avatar
    Join Date
    January 18th, 2005
    Location
    Mansfield, TX
    Posts
    16,158
    Just to exand on that a little further:

    Instead of:

    find . -type f -mtime XXX -maxdepth 1 -exec rm {} \;

    I would recommend something like this:

    find /path/to/specific/directory -type f -mtime +30 -maxdepth 1 -exec rm {} \;

    By specifying the exact directory you want to purge from, you won't accidentally delete from the wrong directory.

    Also, the numeric parameters (like mtime) default to exact if you don't include a sign, greater than if you include +, and less than if you include -. You probably want to delete everything older than a certain age, so you would use something like "+30". For instance:

    -mtime 30 ===> Exactly 30 days old
    -mtime +30 ==> Older than 30 days old
    -mtime -30 ==> Newer than 30 days old

    I'll echo John's statement. Be very, very careful with "find". When you first try it, I would recommend replacing the "-exec rm {}\;" with "-ls". That will just show a directory listing of the affected files instead of removing them.
    Michael Coley
    Amazing-Bargains.com
     Affiliate Tips | Merchant Best Practices | Affiliate Friendly? | Couponing | CPA Networks? | ABW Tips | Activating Affiliates
    "Education is the most powerful weapon which you can use to change the world." Nelson Mandela

  5. #5
    ABW Ambassador
    Join Date
    January 18th, 2005
    Location
    Singapore
    Posts
    1,597
    Thanks. It works like a dream on one of my test sites.

    Any way to qualify it so it only removes *.htm?

  6. #6
    Moderator MichaelColey's Avatar
    Join Date
    January 18th, 2005
    Location
    Mansfield, TX
    Posts
    16,158
    To only include .htm files, add the following:

    -name "*.htm"

    Once again, test it with "-ls" to make sure it returns the results you expect before you turn it loose with "-exec rm {}\;".
    Michael Coley
    Amazing-Bargains.com
     Affiliate Tips | Merchant Best Practices | Affiliate Friendly? | Couponing | CPA Networks? | ABW Tips | Activating Affiliates
    "Education is the most powerful weapon which you can use to change the world." Nelson Mandela

+ Reply to Thread

Similar Threads

  1. Using CRON job to delete zip files
    By Convergence in forum Programming / Datafeeds / Tools
    Replies: 13
    Last Post: July 6th, 2012, 04:44 PM
  2. Replies: 1
    Last Post: May 21st, 2009, 04:14 AM
  3. Delete records from the database using Date()
    By akinak in forum Programming / Datafeeds / Tools
    Replies: 0
    Last Post: February 5th, 2009, 04:50 PM
  4. All your files belong to us - Files Held Ransom
    By zendozen in forum Midnight Cafe'
    Replies: 1
    Last Post: May 26th, 2005, 10:55 AM
  5. Can I delete the huge log files on my server?
    By new yorker in forum Midnight Cafe'
    Replies: 18
    Last Post: September 3rd, 2003, 09:53 AM

Posting Permissions

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