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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to delete files older than X generations...

Status
Not open for further replies.

bondtrails

Technical User
Nov 19, 2002
31
US
Hello all!
I am new to unix scripting and I am trying to learn a few things by writing in unix scripts utility programs I wrote in other languages. Here is where I am currently stumped:

I would like to delete files older than "X" generations. By that I mean if in a given directory there are 10 files (for arguments sake) and I want my script to keep only the first 3 files and delete all other files (therefore deleting files "older" than 3 generations), how would it be done? Note that the file names will always follow a specific format: YYYYMMDD_HHMNSS_FILENAME.txt

I've seen the other posts that use "find <dir> -type f -mtime DD -exec rm {}\;" command but as far as I can tell, this function depends on the number of days DD and not specifically on the broader case of number of generations.

Thank you for any help or direction you can provide!

--Bondster!
 
If there are different txt files with multiple versions:

Code:
for fn in (ls|cut -c17-|sort -u)
do
 ls -t -1 ????????_??????_${fn}|tail +4|xargs rm
done

But experiment a bit with this first - i.e. leave out the "|xargs rm" part first.

HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top