Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting 'old' archived files 2

Status
Not open for further replies.

ddrillich

Technical User
Jun 11, 2003
546
US
Good Day,

We generate the following archived files and directories on a daily basis and we would like to keep only the recent files/directories from the past 10 or 15 days. Is it possible?

-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 354304 Aug 5 23:00 grammar.ecr2009080623:00:01
drwxr-xr-x 2 relatedtopicsadmin relatedtopicsadmin 4096 Aug 5 23:00 lucene.index2009080623:00:01
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 173 Aug 5 23:00 entities.txt2009080623:00:01
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 114183 Aug 5 23:00 grammar.xml2009080623:00:01
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 687523 Aug 5 23:00 lucene.xml2009080623:00:01
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 354304 Aug 4 23:00 grammar.ecr2009080523:00:01
drwxr-xr-x 2 relatedtopicsadmin relatedtopicsadmin 4096 Aug 4 23:00 lucene.index2009080523:00:01
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 173 Aug 4 23:00 entities.txt2009080523:00:01
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 114183 Aug 4 23:00 grammar.xml2009080523:00:01
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 687523 Aug 4 23:00 lucene.xml2009080523:00:01

Regards,
Dan
 
Hi Dan. Man find and the -mtime option to it, together with the -exec option to delete or relocate these files.

Some days are diamonds, some days are rocks - make sure most are the former.
 
Good Day,

One thing is not clear to me. find . -mtime 3 -exec ls -lt '{}' \; returns a very long list of files while find . -mtime 4 -exec ls -lt '{}' \; returns a short list.

The directory holds files from July 18 – Aug 5.

[relatedtopicsadmin@sbkj2kvrtappf01 archive]$ find . -mtime 3 -exec ls -lt '{}' \;
total 19776
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 354304 Aug 5 23:00 grammar.ecr2009080623:00:01
drwxr-xr-x 2 relatedtopicsadmin relatedtopicsadmin 4096 Aug 5 23:00 lucene.index2009080623:00:01
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 173 Aug 5 23:00 entities.txt2009080623:00:01
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 114183 Aug 5 23:00 grammar.xml2009080623:00:01
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 687523 Aug 5 23:00 lucene.xml2009080623:00:01
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 354304 Aug 4 23:00 grammar.ecr2009080523:00:01
drwxr-xr-x 2 relatedtopicsadmin relatedtopicsadmin 4096 Aug 4 23:00 lucene.index2009080523:00:01
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 173 Aug 4 23:00 entities.txt2009080523:00:01
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 114183 Aug 4 23:00 grammar.xml2009080523:00:01
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 687523 Aug 4 23:00 lucene.xml2009080523:00:01
-rw-rw-r-- 1 relatedtopicsadmin relatedtopicsadmin 354304 Aug 4 16:04 grammar.ecr2009080423:00:02
drwxrwxr-x 2 relatedtopicsadmin relatedtopicsadmin 4096 Aug 4 16:04 lucene.index2009080423:00:02
-rw-rw-r-- 1 relatedtopicsadmin relatedtopicsadmin 173 Aug 4 16:04 entities.txt2009080423:00:02
-rw-rw-r-- 1 relatedtopicsadmin relatedtopicsadmin 114183 Aug 4 16:04 grammar.xml2009080423:00:02
-rw-rw-r-- 1 relatedtopicsadmin relatedtopicsadmin 687523 Aug 4 16:04 lucene.xml2009080423:00:02
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 355328 Aug 3 23:00 grammar.ecr2009080416:04:02
drwxr-xr-x 2 relatedtopicsadmin relatedtopicsadmin 4096 Aug 3 23:00 lucene.index2009080416:04:02
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 173 Aug 3 23:00 entities.txt2009080416:04:02
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 114177 Aug 3 23:00 grammar.xml2009080416:04:02
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 687518 Aug 3 23:00 lucene.xml2009080416:04:02
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 355840 Jul 31 23:00 grammar.ecr2009080323:00:01
drwxr-xr-x 2 relatedtopicsadmin relatedtopicsadmin 4096 Jul 31 23:00 lucene.index2009080323:00:01
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 173 Jul 31 23:00 entities.txt2009080323:00:01
.....

While -

[relatedtopicsadmin@sbkj2kvrtappf01 archive]$ find . -mtime 4 -exec ls -lt '{}' \;
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 354304 Aug 5 23:00 ./grammar.ecr2009080623:00:01
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 114183 Aug 5 23:00 ./grammar.xml2009080623:00:01
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 173 Aug 5 23:00 ./entities.txt2009080623:00:01
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 687523 Aug 5 23:00 ./lucene.xml2009080623:00:01
total 336
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 60823 Aug 5 23:00 _0.cfs
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 248370 Aug 5 23:00 _0.cfx
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 62 Aug 5 23:00 segments_2
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 20 Aug 5 23:00 segments.gen
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 60823 Aug 5 23:00 ./lucene.index2009080623:00:01/_0.cfs
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 20 Aug 5 23:00 ./lucene.index2009080623:00:01/segments.gen
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 248370 Aug 5 23:00 ./lucene.index2009080623:00:01/_0.cfx
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 62 Aug 5 23:00 ./lucene.index2009080623:00:01/segments_2
[relatedtopicsadmin@sbkj2kvrtappf01 archive]$
 
The "-mtime 3" will give you files that are exactly three days old. If you want it to be "older than three days", you need to make it "-mtime +3". Or "three days and older" (to include 4 day old files), make it "-mtime +2".

See the man page for "find" for more info.


 
Glad to help Dan. Sorry it was a rather cryptic answer, I was on my way out of the door at the time!

Some days are diamonds, some days are rocks - make sure most are the former.
 
Thank you for your help so far.

Since we don't necessarily generate these files every day, we changed a bit the objective - we would like to keep around, at any given time, the last 10 files.
So, for example, we have the entities files. We would like to delete all entities files except the most recent 10 of them. Is it possible?

[relatedtopicsadmin@sbkj2kvrtappf01 archive]$ ls -lt e*
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 173 Aug 5 23:00 entities.txt2009080623:00:01
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 173 Aug 4 23:00 entities.txt2009080523:00:01
-rw-rw-r-- 1 relatedtopicsadmin relatedtopicsadmin 173 Aug 4 16:04 entities.txt2009080423:00:02
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 173 Aug 3 23:00 entities.txt2009080416:04:02
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 173 Jul 31 23:00 entities.txt2009080323:00:01
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 173 Jul 30 23:00 entities.txt2009073123:00:01
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 173 Jul 29 23:00 entities.txt2009073023:00:01
-rw-rw-r-- 1 relatedtopicsadmin relatedtopicsadmin 173 Jul 29 18:25 entities.txt2009072923:00:01
-rw-rw-r-- 1 relatedtopicsadmin relatedtopicsadmin 173 Jul 29 10:30 entities.txt2009072918:25:39
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 69 Jul 28 23:00 entities.txt2009072910:30:55
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 69 Jul 27 23:00 entities.txt2009072823:00:01
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 69 Jul 24 23:00 entities.txt2009072723:00:01
-rw-rw-r-- 1 relatedtopicsadmin relatedtopicsadmin 113 Jul 24 14:42 entities.txt2009072423:00:01
-rw-rw-r-- 1 relatedtopicsadmin relatedtopicsadmin 69 Jul 24 14:40 entities.txt2009072414:42:14
-rw-rw-r-- 1 relatedtopicsadmin relatedtopicsadmin 113 Jul 24 14:35 entities.txt2009072414:40:21
-rw-rw-r-- 1 relatedtopicsadmin relatedtopicsadmin 69 Jul 24 10:35 entities.txt2009072414:35:06
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 69 Jul 23 23:00 entities.txt2009072410:35:39
-rw-rw-r-- 1 relatedtopicsadmin relatedtopicsadmin 69 Jul 23 13:00 entities.txt2009072323:00:01
-rw-rw-r-- 1 relatedtopicsadmin relatedtopicsadmin 69 Jul 22 14:54 entities.txt2009072313:00:31
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 69 Jul 22 14:06 entities.txt2009072214:54:42
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 69 Jul 21 14:06 entities.txt2009072214:06:01
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 69 Jul 20 14:06 entities.txt2009072114:06:01
-rw-r--r-- 1 relatedtopicsadmin relatedtopicsadmin 69 Jul 20 14:04 entities.txt2009072014:06:01
-rw-rw-r-- 1 relatedtopicsadmin relatedtopicsadmin 94 Jul 20 13:37 entities.txt2009072014:04:01
-rw-rw-r-- 1 relatedtopicsadmin relatedtopicsadmin 94 Jul 18 17:56 entities.txt2009072013:37:11


 
Since the files have the dates in their name in the form, YYYYMMDD, you can sort by file name to get the files you want...
Code:
ls -1 entities.txt2* | sort -r | tail +11 | xargs rm
That will delete all but the latest 10 files. And "latest" is determined by the date and time in the file name.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top