Date within a timeframe 2 days ago


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date within a timeframe 2 days ago
# 8  
Old 05-14-2010
I do see a potential need for this sort of peg approach, especially if even a cron job misses or similar; or you'd need to just re-do the job. It would allow you to rely on a fixed timeframe, but methyl's point is well-taken since your point in the day would also need to be pegged to a difference between now and 6am.

A better approach would probably be to dummy up your timestamps and then use these within your find query string; I've always needed (and done) this in SQL, but I've never hunkered down to do it within the shell for files. I'm on Solaris, so GNU date is not an option, I'm using the standard issue ksh88 (on Solaris 10) or ksh93 (on Windows) and perl is not a viable option for the OP. So the question is really more over basic portability: can the shell manage a suitable date calculation method? And if it can, what's the most portable approach across the various shells?

Okay, so if you were able to pony up a reliable calculation of today's date-1 and date-2 (or better yet...any date-1 and date-2 args, to allow for any sort of catch-up situation...), you could effectively dummy up a timestamp and touch two files for use within the find command to use a query against -newer $date-1 and ! -newer $date-2. Since the available options will vary by your platform and/or shell, the components may vary, but the results shouldn't. Based on your original find command query's use of -mmin, it looks like you're either GNU or ksh93-based, so you might be able to use kshji's -d option, but this is more of a mental exercise for me. (I'd argue using perl is good for you, but there are many ways to get the required results.)

I'll take the wimp's way out for the moment and propose a pseudocode theory as opposed to actual code here, since I am crunched for time today, but the basic premise is as follows. (I'll provide actual code at some point, if I get a chance):

0) Calculate your timespan based on arguments to a function/script (ie, 2 and 1 to achieve two days ago and one day ago, etc);
-> this allows for flexibility in terms of days back, etc. (Can be expanded to apply to the specific timestamp as well...)

1) Take those calculated dates and dummy them up into a touch command such as follows:
-> note the use of literals in the timestamps, which allows for the inclusion of another variable-driven value;

Code:
         touch /tmp/beg_date.txt 
         touch /tmp/end_date.txt 
         touch -m ${1:-$(date +%m%d0600)} /tmp/beg_date.txt    #  $1 = MMDDHHmm 
         touch -m ${2:-$(date +%m%d0559)} /tmp/end_date.txt    #  $2 = MMDDHHmm

2) Modify your find command to reflect these bookend files in your query:

Code:
         find . -newer /tmp/beg_date.txt -a ! -newer /tmp/end_date.txt | ...

I'd also recommend the following link with regard to going about actually calculating the days back value(s), since this would be specific to your platform and/or preferences (https://www.unix.com/answers-frequent...rithmetic.html)

Last edited by curleb; 05-14-2010 at 03:19 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get a given date and subtract it to 5 days ago

Hi all, I have been researching to obtain SSL certification expiry for most of our webistes. For some cases, some hosts where not directly accessible so i finally got a solution working with curl using my proxy. This lists the expiry date which i'm finally looking for. # curl --proxy... (4 Replies)
Discussion started by: nms
4 Replies

2. UNIX for Beginners Questions & Answers

How to find a file that's modified more than 2 days ago but less than 5 days ago?

How to find a file that's modified more than 2 days ago but was modified less than 5 days ago by use of any Linux utility ? (4 Replies)
Discussion started by: abdulbadii
4 Replies

3. Shell Programming and Scripting

Calculating 7 days ago date for the given Argument

Hi I have shell script and I am facing the below issue to integrate the date calculation to the the script. If I give the $1 as the date(20110701) then I need to get the 7 days ago date for the same format.(20110624). At first I thought its a simple one to handle and I did a search in the... (10 Replies)
Discussion started by: filter
10 Replies

4. UNIX for Advanced & Expert Users

N days ago

Hi, the following gives today $(date '+%d%m%y') For example 210111 for today (21 of january 2011). How can I have n days ego ? For example 160111 for 5 days ego ? thank you. (3 Replies)
Discussion started by: big123456
3 Replies

5. UNIX for Dummies Questions & Answers

Clarification on '1 days ago' in date command [Found answer, posted within]

I know the topic of getting yesterday's date has been covered ad nauseum, but I just want to be clear on something. I recently started using the command date --date='1 days ago' '+%m/%d/%y' to get yesterday's date and it's been working great. I just want to be certain that it is going to... (1 Reply)
Discussion started by: DeCoTwc
1 Replies

6. Shell Programming and Scripting

how to get what date was 28 days ago of the current system date IN UNIX

Hi, Anybody knows how to get what date was 28 days ago of the current system date through UNIX script. Ex : - If today is 28th Mar 2010 then I have to delete the files which arrived on 1st Mar 2010, (15 Replies)
Discussion started by: kandi.reddy
15 Replies

7. Shell Programming and Scripting

date for two days or 3 days ago

i need a script that can tell me the date 2 days ago or 3 days ago. please help (7 Replies)
Discussion started by: tomjones
7 Replies

8. Shell Programming and Scripting

Find the file from 15 days ago

How can I get difference date between today and 15 days ago and all filename is was created before 15 days ago? It has to be korn shell script. Thanks. (2 Replies)
Discussion started by: YoungBlood
2 Replies

9. UNIX for Dummies Questions & Answers

file was created before 15 days ago.

How can I get difference date between today and 15 days ago and all filename is was created before 15 days ago? It has to be korn shell script. Thanks. (1 Reply)
Discussion started by: YoungBlood
1 Replies

10. Shell Programming and Scripting

Function 7 days ago

Please tell me how can I wirte a function to return the date 7 days ago by using calendar command? :confused: (2 Replies)
Discussion started by: LAY
2 Replies
Login or Register to Ask a Question