Deleting files after x days from the latest date file
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
The problem that you face here is that Linux and Unix do not record the creation date of a file. The date that the file was last accessed and the date that the file was last modified are saved. You can either depend on one of those settings with the chance that old files will not be removed because they were last modified or accessed some time since they were created or you can record the creation date inside the file. I would choose to use the last modified date as the criteria. In that case you can use the find command to find all of the files in a directory tree that were modified more than 60 days ago, for example. The syntax of the find command is:
So if you want to search the /var/log/mylogs directory tree for all files that were modified more than 60 days ago and delete these files the command would look like this.
Code:
find /var/log/mylogs -type f -ctime +1440 -exec rm {} \;
Here the ctime is measured in hours so 60 days times 24 hours is 1440. The plus sign in front of the 1440 means "more than". So this command will find files that were last modified/changed more than 60 days ago and it will remove them.
More information about the find command is available in the man pages.
Code:
man find
Last edited by stress_junkie; 03-01-2007 at 07:56 PM.
1) Find the latest date file
Say 2003-march-21 is the last file available in that directory,
then it should look for files older than that for 2 months and delete it.
Which means it should delete 2003-jan-21 and above files
I hope you are getting it this time.
Heres a script I have worked out on which finds the latest date and deletes restall file, but I want it to delete x no of days from the latest date file :
bash-3.00$ cat script.sh
#!/bin/bash
Keepdate=`/bin/ls -ult --full-time| sed 1d | head -1 | awk '{ print $6 }'`
Unix shell does NOT do date arithemetic very well.
Write a script:
Code:
1. find newest *.log file in the /path: youngfile= `ls -l -rt | tail -1`
2. use datecalc to take your filedate, subtract 60 days from it, and
return another date 60 days further in the past.
3. touch -t <older date> ./testfile
4. find /path ! -newer ./testfile -name '*.log' -exec rm -f {{} \;
Repeat steps 1 -4 for each directory
It is clear to me that you aren't "getting" the solution that I proposed and the reasons behind it. Your script is no better than my solution but it is a lot more complicated. Your script is still using the last modified date of the files to select the ones to delete.
Last edited by stress_junkie; 03-01-2007 at 07:52 PM.
I agree that trusting the times (ctime ,mtime, atime) can lead to incorrect (and worse, unwanted) removals, although it can be convenient to code. Solutions that use a time-stamp recorded as data in a log file are less risky, but they require more thought and work. One would not necessarily need to have only the most recent time, but one could keep track of any file and its creation date.
I think that sriramsreedhars was not being rude. I suspect that English is not his first language, and that he was using a phrase that was neutral to him.
My practice for associating creation dates with important files and directories is to make the creation date part of the name, e.g.
Code:
/backup/debian/makyo/2007-03-03
This makes date calculations with the arithmetic facilities of GNU/Linux command date very easy -- although -- as I customarily rant -- those capabilities are not noted in the man page for date, but rather as a few examples in:
Code:
info coreutils date
If portability is a concern, Dave Taylor posted a short c code that may serve: http://www.askdavetaylor.com/date_ma...ll_script.html , and once you test it, you can modify it to eliminate the explanatory string to get simply the date calculation (a small effort is required).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.