LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-13-2006, 10:49 PM   #1
anaik100
LQ Newbie
 
Registered: Nov 2006
Posts: 5

Rep: Reputation: 0
timestamp


my logic is
current timrstamp - file stamp > 15
then delete this file

needed help to write a script for above logic
 
Old 11-14-2006, 04:19 AM   #2
imagineers7
Member
 
Registered: Mar 2006
Distribution: BackTrack, RHEL, FC, CentOS, IPCop, Ubuntu, 64Studio, Elive, Dream Linux, Trix Box
Posts: 310

Rep: Reputation: 30
hi anaik100,


find <path of dir> -mtime -15 -name "*" -exec rm '{}' \; -print

Will search for all files under the <path> which have been modified within 15 days and then will delete them,

Hope, This hint will help you





Aniruddha
 
Old 11-14-2006, 09:21 AM   #3
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
Yep, find will do it without a script.

The test -name "*" has the effect of excluding files that start with a period ("."). Anaik100 didn't post this restriction, so be sure it is intended.

If there are many files, forking off a subprocess using -exec can be slow. If that's an issue,
Code:
find directory -mtime 15 | xargs rm
If you want to see what gets deleted (as with -print above), use rm -v.
 
Old 11-14-2006, 09:59 AM   #4
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 269Reputation: 269Reputation: 269
When I want to just delete files, I'll usually add a -type f so it doesn't remove directories I want to keep in place, just a little extra precaution I use.
 
Old 11-14-2006, 01:40 PM   #5
anaik100
LQ Newbie
 
Registered: Nov 2006
Posts: 5

Original Poster
Rep: Reputation: 0
i want to delete the files which were created before 15 days. The script deletes the file which were created in last 15 days.

Please clarify
 
Old 11-14-2006, 02:09 PM   #6
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
man find

-15 (as the first poster suggested) means "less than 15 days ago". 15 (as I accidentally wrote) means "exactly 15 days ago", that is 15 24-hour periods, omitting any fractional part. Obviously it should have been "more than 15 days ago", i.e.
Code:
find directory -type f -mtime +15 | xargs rm
Taking trickyid's advice. BTW, while experimenting, rm -i is safer than rm.
 
Old 11-15-2006, 02:00 AM   #7
imagineers7
Member
 
Registered: Mar 2006
Distribution: BackTrack, RHEL, FC, CentOS, IPCop, Ubuntu, 64Studio, Elive, Dream Linux, Trix Box
Posts: 310

Rep: Reputation: 30
Hi,


Quote:
Originally Posted by QUIGI
find directory -type f -mtime +15 | xargs rm
But, if the no. of arguments ie. no of files find produces is larger, bash will give errors that argument list is to much large.

So, it is better to operate one file at a time using -exec or -ok. Because I belive that both the switches work one argument at a time.

I am not sure about -exec / -ok 's behaviour about that but it does not produce argument list is too much large error. Correct me if I am wrong.


Thanks & Regards,



Aniruddha
 
Old 11-15-2006, 08:56 AM   #8
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
If resource consumption is no problem, go with -exec.

You can limit the length of the command line / argument list by using the option --max-chars or --max-args or --max-lines to xargs. It will then invoke rm repeatedly, but you'll still save, because it can remove many files at a time.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
about timestamp George2 Programming 1 08-16-2006 09:13 AM
Timestamp ancys Programming 5 08-07-2006 09:21 AM
timestamp cj24900 Linux - General 7 03-17-2006 12:05 PM
Timestamp Help jch02140 Linux - Newbie 7 03-17-2006 02:33 AM
sql timestamp help vickr1z Linux - Newbie 6 10-12-2004 08:24 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration