Date conversion and compare


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Date conversion and compare
# 1  
Old 12-30-2010
Question Date conversion and compare

I want to find all files in current directory having last modified date less than specific date which I am getting as output of some command in "MMM DD" format... Problem is, I am not able to compare this date with ls -l output. I am also not able to use find command as I am not able to convert date from string format... Kindly let me know if anyone knows...
# 2  
Old 12-30-2010
"some command" produces a date. What is the "some command"? Commands like date let you specify what you get.
# 3  
Old 12-30-2010
Attempting to interpret what is needed -- list of files that are older than a specific date/time:
Code:
#! /bin/ksh

NAME=$(basename ${0})
FILE=$(mktemp --tmpdir ${NAME}.XXXXXX)

trap "rm -f ${FILE}" EXIT

TIME="${1}"
shift

touch -t "${TIME}" ${FILE}
find "${@}" -type f ! -newer ${FILE} -print

The first argument is the time in the format [[CC]YY]MMDDhhmm[.ss], the second and subsequent arguments are directories.
This User Gave Thanks to m.d.ludwig For This Post:
# 4  
Old 12-31-2010
Java

Quote:
Originally Posted by m.d.ludwig
Attempting to interpret what is needed -- list of files that are older than a specific date/time:
Code:
#! /bin/ksh
 
NAME=$(basename ${0})
FILE=$(mktemp --tmpdir ${NAME}.XXXXXX)
 
trap "rm -f ${FILE}" EXIT
 
TIME="${1}"
shift
 
touch -t "${TIME}" ${FILE}
find "${@}" -type f ! -newer ${FILE} -print

The first argument is the time in the format [[CC]YY]MMDDhhmm[.ss], the second and subsequent arguments are directories.


Idea looks good - make a temp file with that date and then use find command to search required files....

But, making temp file requires time in MMDDhhmm format and we have time in "MMM DD" format ("Dec 20"). I don't want to write a function to convert externally
Jan -> 01
Feb -> 02
...
...
Do we have any built in conversion function?
Also,
I want to find files older than specified date, not newer files.... In find command, we can use "-newer" option but we don't have "-older" option.

---------- Post updated at 12:40 PM ---------- Previous update was at 12:33 PM ----------

Quote:
Originally Posted by jim mcnamara
"some command" produces a date. What is the "some command"? Commands like date let you specify what you get.

"Some command" - we have a local command which produces output in "MMM DD" format (eg "Dec 20"). Using this, I want to schedule a cron to move older files to archive directory...
# 5  
Old 12-31-2010
What Operating System and version are you running?
What Shell are you using?
Do you have a High Level Programming Language available?
Do you have use of "perl", "awk" or other quick programming tools?

Further to Jim Mcnamara, what is the "some command" ? Is it something which can be changed to produce a date in a more usable format - preferably including the year too? Is it written in a language which could be used to convert the date? Is the date output from "some command" created on that very day (in which case the process could perhaps output a timestamp file)?

If the date format cannot be changed what is the precise format for a single digit day (leading zero?, month two spaces day?, month space day?).

To answer one of you questions in mainstream "find" the effect of "-older" is achieved by negating "-newer". If your date actually comes from "ls" command we could of course refer to the file itself not a directory listing of the file.


Example of one way to convert character month to numeric month:
This script is an example which also tests itself for every possible month name. There are other more clever Shell techniques available which are less portable.
Code:
for MMM in "Jan" "Feb" "Mar" "Apr" "May" "Jun"\
           "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
do
        case "${MMM}" in
                "Jan")  MM="01" ;;
                "Feb")  MM="02" ;;
                "Mar")  MM="03" ;;
                "Apr")  MM="04" ;;
                "May")  MM="05" ;;
                "Jun")  MM="06" ;;
                "Jul")  MM="07" ;;
                "Aug")  MM="08" ;;
                "Sep")  MM="09" ;;
                "Oct")  MM="10" ;;
                "Nov")  MM="11" ;;
                "Dec")  MM="12" ;;
        esac
        #
        echo "${MMM}:${MM}"
done

Jan:01
Feb:02
Mar:03
Apr:04
May:05
Jun:06
Jul:07
Aug:08
Sep:09
Oct:10
Nov:11
Dec:12

This User Gave Thanks to methyl For This Post:
# 6  
Old 12-31-2010
If you read the touch manpage, you will see that the --date option will take human-friendly dates.
# 7  
Old 01-04-2011
Looks like I will be able to complete desired job with the details provided in this forum. Thank you all. Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to compare two files of todays date and yesterday's date

hi all, How to compare two files whether they are same are not...? like i had my input files as 20141201_file.txt and 20141130_file2.txt how to compare the above files based on date .. like todays file and yesterdays file...? (4 Replies)
Discussion started by: hemanthsaikumar
4 Replies

2. Shell Programming and Scripting

Date Conversion

Hi I want to convert date from one format to the other i.e. From Thu Mar 6 15:57:39 2014 To 2014-03-06 15:57:39 Is there any direct command to implement this ? (1 Reply)
Discussion started by: tostay2003
1 Replies

3. AIX

Date Conversion

Hi, I would like to change the incoming date format (Jun 25 2010) to 06/25/2010. I have seen date "dconv" function but i am not sure how that works. $ echo "Jun 25 2010" | nawk ' { months=" JanFebMarAprMayJunJulAugSepOctNovDec";date=$1;month=index(months,substr($1,1,3))/3; ... (3 Replies)
Discussion started by: Prashanth B
3 Replies

4. Shell Programming and Scripting

ksh compare dates INSIDE a file (ie date A is > date B)

In KSH, I am pasting 2 almost identical files together and each one has a date and time on each line. I need to determine if the first instance of the date/time is greater than the 2nd instance of the date/time. If the first instance is greater, I just need to echo that line. I thought I would... (4 Replies)
Discussion started by: right_coaster
4 Replies

5. Shell Programming and Scripting

Julian date to Calendar date conversion

Hi all, I require to convert julian date to normal calander date in unix for eg julian date=122 now i want corresponding calander date ---------------------------------------- gr8 if give very small command/script and please explain the steps as well(imp) Thanks ... (3 Replies)
Discussion started by: RahulJoshi
3 Replies

6. UNIX for Dummies Questions & Answers

Date conversion in ab i

(string(8)) ((date("YYYYMMDD")) ((date("YYYY/MM/DD")) in.date_field_name)) (1 Reply)
Discussion started by: dr46014
1 Replies

7. Shell Programming and Scripting

Conversion of date to Julian date

Hi Gurus, Need help in Conversion of date(2007-11-30) to Julian date(YYDDD)... '+%J' 2007-11-30 to 'YYDDD' Thanks (4 Replies)
Discussion started by: SeenuGuddu
4 Replies

8. Shell Programming and Scripting

Compare date from db2 table to yesterday's Unix system date

I am currently running the following Korn shell script which works fine: #!/usr/bin/ksh count=`db2 -x "select count(*) from schema.tablename"` echo "count" I would like to add a "where" clause to the 2nd line that would allow me to get a record count of all the records from schema.tablename... (9 Replies)
Discussion started by: sasaliasim
9 Replies

9. Shell Programming and Scripting

date conversion

file1 E106,0,1/9/1993,0,E001,E003,A,45200,3766.667,21.730769 E108,0,2/3/1995,0,E001,E003,A,15000,1250,7.211538 E109,0,06-mar-07,0,E001,E001,A,78000,6500,37.5 E110,0,09-dec-2008,0,E001,E001,A,56000,4666.667,26.923077 E104,0,06/04/1994,0,E001,E003,A,95000,7916.667,45.673077... (14 Replies)
Discussion started by: charandevu
14 Replies

10. UNIX for Advanced & Expert Users

Date Conversion

Hello, I want to convert MM DD YYYY date format to MM-DD-YYYY format. For exemple: I have to convert Nov 28 2005 to 28-11-2005. Thenks for youf help. DAFI (2 Replies)
Discussion started by: dafidak
2 Replies
Login or Register to Ask a Question