comparing time stamps

Hello All,

I'm generating timestamps (file creation timestamps) for all the files in a directory. I need to compare all the timestamps.
for example if i have 4 files and their timestamps are 20091125114556,
20091125114556,20091125114556,20091125114556 respectively.

I need to differentiate atleast secs portion to recognize the file. So what i'm going to do is if all timestamps are same then i will modify seconds portion. But my question here is how to compare these timestamps respect to their corresponding files.?

Please suggest a way but not the solution.

Thanks in advance,
RSC1985

you are comparing with what?

where the timestamps present? in the filename ?

Comparing with the other timestamps of other files. And i'm storing timeStamp in a variable as of now. My questions is how to acheive that comparison task?

Search a bit, this has been answered before.
I will give you a hint how you can do this:

  1. separte the YYYYMMDD and HHMISS parts
  2. Compare the YYYYMMDD if they are same
  3. compare HHMISS
    this way you can know whether the timestamps are same or not... rest is just the coding manipulation.

Can't I do direct comparison Because if i store into a variable the timestamp, I can directly do the comparison. But I'm struggling to store all these timestamps and next comparing all these timestamps one with the other.I should get how many are same and what are those so that i can go with my rest of the processing.If could give some more idea on this it would be really helpful.

can you post specific samples so we understand the problem better?

Below is the example.

I will get the timestamps(file modification time in the form YYYYMMDDHHMMSS) for all the files in the directory by the below code.

for filename in `find . -type f -exec basename {} \;`
do
echo "The file name is $filename"
timestamp=`../../tmp/getTime.pl $filename`
echo "the timestamp of $filename is $timestamp"
done

Now I have to check for what are the files that have same timestamps.
for example file1 has 20091225114556 timestamp and file3 has 20091225114556 timestamp and file4 has also 20091225114556 the same timestamp. My task is to modify these timestamps inorder to make them unique.
So i need to modify the timestamp of file3 and file4 as 20091225114557 and 20091225114558.

could you please give an idea how to proceed on this?

you can try this out.. this is not a very good script, just to give you an idea.

the first search sort all files based on time
it's hard to add "1" in the seconds since it will require time calculation.
i have used touch -t to change the timestamp of the file to current time instead.

the time format of touch -t is [[CC]YY]MMDDhhmm[.SS]
if you can calculate the time of the previous timestamp to add just 1 sec then your good to go.

timestamp_old="20090101010101"

for filename in `ls -lrt | grep \^- | awk '{print $9}'`
do
	timestamp=`date +"%Y%m%d%H%M.%S"`
	echo "The file name is $filename"
	timestamp_new=`../../tmp/getTime.pl $filename`
	echo "the timestamp of $filename is $timestamp_new"

	if [ $timestamp_new = $timestamp_old ];then
		touch -t "$timestamp" $filename
	fi
	
	timestamp_old=$timestamp_new
done

Thanks a ton for the idea. changing the timestamp using touch command will definitely help me going forward. Once again thanks a lot for the useful reply.

i think i just found a solution for you.
i use date -r for epoch time, date -d to calculate time. i just added 1 second to modified epoch time of the file and convert the time.

timestamp_old="0"

for filename in `ls -lrt | grep \^- | awk '{print $9}'`
do
	echo "The file name is $filename"
	#timestamp_new=`../../tmp/getTime.pl $filename`
	timestamp_new=`date +"%s" -r $filename`
	echo "the timestamp of $filename is $timestamp_new seconds pass 1970-01-01"

	if [ $timestamp_new = $timestamp_old ];then
		timestamp_new=`expr $timestamp_new + 1`
		timestamp=`date -d "1970-01-01 $timestamp seconds" +"%Y%m%d%H%M.%S"`
		touch -t "$timestamp" $filename
	fi
	
	timestamp_old=$timestamp_new
done