Sponsored Content
Top Forums Shell Programming and Scripting Date within a timeframe 2 days ago Post 302421522 by curleb on Friday 14th of May 2010 02:12:09 PM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
dos2unix(1)						      General Commands Manual						       dos2unix(1)

NAME
dos2unix - DOS/MAC to UNIX text file format converter SYNOPSYS
dos2unix [options] [-c convmode] [-o file ...] [-n infile outfile ...] Options: [-hkqV] [--help] [--keepdate] [--quiet] [--version] DESCRIPTION
This manual page documents dos2unix, the program that converts plain text files in DOS/MAC format to UNIX format. OPTIONS
The following options are available: -h --help Print online help. -k --keepdate Keep the date stamp of output file same as input file. -q --quiet Quiet mode. Suppress all warning and messages. -V --version Prints version information. -c --convmode convmode Sets conversion mode. Simulates dos2unix under SunOS. -o --oldfile file ... Old file mode. Convert the file and write output to it. The program default to run in this mode. Wildcard names may be used. -n --newfile infile outfile ... New file mode. Convert the infile and write output to outfile. File names must be given in pairs and wildcard names should NOT be used or you WILL lost your files. EXAMPLES
Get input from stdin and write output to stdout. dos2unix Convert and replace a.txt. Convert and replace b.txt. dos2unix a.txt b.txt dos2unix -o a.txt b.txt Convert and replace a.txt in ASCII conversion mode. Convert and replace b.txt in ISO conversion mode. Convert c.txt from Mac to Unix ascii format. dos2unix a.txt -c iso b.txt dos2unix -c ascii a.txt -c iso b.txt dos2unix -c mac a.txt b.txt Convert and replace a.txt while keeping original date stamp. dos2unix -k a.txt dos2unix -k -o a.txt Convert a.txt and write to e.txt. dos2unix -n a.txt e.txt Convert a.txt and write to e.txt, keep date stamp of e.txt same as a.txt. dos2unix -k -n a.txt e.txt Convert and replace a.txt. Convert b.txt and write to e.txt. dos2unix a.txt -n b.txt e.txt dos2unix -o a.txt -n b.txt e.txt Convert c.txt and write to e.txt. Convert and replace a.txt. Convert and replace b.txt. Convert d.txt and write to f.txt. dos2unix -n c.txt e.txt -o a.txt b.txt -n d.txt f.txt DIAGNOSTICS
BUGS
The program does not work properly under MSDOS in stdio processing mode. If you know why is that so, please tell me. AUTHORS
Benjamin Lin - <blin@socs.uts.edu.au> Bernd Johannes Wuebben (mac2unix mode) <wuebben@kde.org> MISCELLANY
Tested environment: Linux 1.2.0 with GNU C 2.5.8 SunOS 4.1.3 with GNU C 2.6.3 MS-DOS 6.20 with Borland C++ 4.02 Suggestions and bug reports are welcome. SEE ALSO
unix2dos(1) mac2unix(1) 1995.03.31 dos2unix v3.0 dos2unix(1)
All times are GMT -4. The time now is 10:29 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy