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

Calculate number of days between two days on sun solaris only

Status
Not open for further replies.

jmarioscs

MIS
Oct 16, 2012
3
BR
Hello!

Must list "n" files from a given directory (exe: / export / home / test /), to recover part of the file names in a variable and then compare the variable with a specific date.

Eg

I have the file TEST-2012-09-18.LOG, just need to assign the value to a particular variable 09.18.2012 and subsequently compare this variable (date) with the current date, if the difference is more than 30 days, I delete the file.

I await return of you!!

hugs
 
The find command I believe will do what you want. below is the command that I use to search for specific files that are older than 30 days and delete them. In my case I am only looking at files in the directory named backup and sub directories for files that start with the letters xln.

find /backup/* -name xln* -mtime +30 -exec rm -f {} \; >/dev/null 2>&1
 
Tolson's solution assumes the last modification times of the files matches the dates in the filenames; if that is the case then it is the best way to go about it.

Otherwise you may find this ksh function useful for your date calculations.

Personally I usually use perl to do date calculations with the C-like POSIX functions, for example this will return the date 30 days ago:

Code:
perl -e 'use POSIX;($d,$m,$y)=(localtime time)[3..5];print strftime "%Y-%m-%d\n",localtime(mktime(0,0,0,$d-30,$m,$y));'

If you choose to use that, I would extract the date from the filename without dashes and just do a numerical comparison, e.g. if (20120913 < 20120917) then delete it.

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top