Welcome, guest! Login
Close

Login to Your Account


  |  » Forgot your password?
Join

Notices

Help. How to delete files before a certain date in Linux?

 
Tools Search
  #1  
Old
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  
Old
Fear and Arrogance
Join Date: January 18th, 2005
Location: Pittsburgh
Posts: 485
style="margin:20px; margin-top:5px; ">
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  
Old
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.
Join ABW to remove this sponsored message.
  #4  
Old
Moderator
Join Date: January 18th, 2005
Location: Mansfield, TX
Posts: 16,080
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  
Old
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  
Old
Moderator
Join Date: January 18th, 2005
Location: Mansfield, TX
Posts: 16,080
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
Join ABW to remove this sponsored message.
Reply
 

Tools Search
Search:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
All your files belong to us - Files Held Ransom zendozen Midnight Cafe' 1 May 26th, 2005 10:55 AM
Manual or Automated Updates? MichaelColey Programming / Datafeeds / Tools 2 July 14th, 2004 07:41 AM
Kaz**=Virus=Spammer brianboru Midnight Cafe' 0 January 9th, 2004 11:01 AM
Can I delete the huge log files on my server? new yorker Midnight Cafe' 18 September 3rd, 2003 09:53 AM

X

Welcome to ABestWeb.com

Create your username to jump into the discussion!

ABestWeb.com is the largest affiliate marketing community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?



Content Relevant URLs by vBSEO ©2011, Crawlability, Inc.