Time Difference between two files in two different directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Time Difference between two files in two different directories
# 1  
Old 09-16-2008
Time Difference between two files in two different directories

Hi,

Can anyone please help me.
How to write shell script for taking time difference between two files which are in two different folders.

Example:

Folder1 is having sample_1_*.txt with the time 13:10 hours
Folder2 is having sample_1_*.txt with the time 13:17 hours

Now i need the time difference between many files like this by running a shell script. I need the output as below

Folder1 Folder2 Time Diff
sample_1_ sample_2_ 7 Seconds

This script is very urgent for me. Anyone's help is honestly appreciated.



Regards,
Sanjay...
# 2  
Old 09-16-2008
What OS are you running?
# 3  
Old 09-16-2008
Try something like below which uses epoch time to calculate the time diff:

Code:
file1_time=$(date +%s -d"$( ls -l file1 | awk ' { print $(NF-3)" "$(NF-2)" "$(NF-1); }')")
file2_time=$(date +%s -d"$( ls -l file1 | awk ' { print $(NF-3)" "$(NF-2)" "$(NF-1); }')")
(( diff_time =  file2_time - file1_time ))
echo $diff_time

# 4  
Old 09-16-2008
Hammer & Screwdriver Here is an approach using the stat command

First, show the last modification time for each file

Code:
> ls -l master.fmt
-rwxrwx--- 1 user xx 658 Sep  2 07:09 master.fmt*
> ls -l wor20080914.fmt
-rw-rw---- 1 user xx 219 Sep 15 11:01 wor20080914.fmt

Then, do stat commands on each file, and subtract one from the other.

Code:
> echo `stat -c%Y wor20080914.fmt` - `stat -c%Y master.fmt` | bc
1137073

You could divide by 60 to learn the # minutes
Divide again by 60 to learn # hours
And divide by 24 to learn the # days == 13.16
# 5  
Old 09-16-2008
stat assumes GNU tools. Not everyone has those. Just so you guys are aware of this.
Not that stat is a bad choice, it is great if you have it.
# 6  
Old 09-17-2008
OS is AIX
# 7  
Old 09-17-2008
AIX has an alternative: the istat command.

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Archiving and moving files into directories, creating directories, etc.

how can i move "dataName".sql.gz into a folder called 'database' and then move "$fileName".tar.gz * .htaccess into a folder called 'www' with the entire gzipped file being "$fileName".tar.gz? Is this doable or overly complex. so mydemo--2015-03-23-1500.tar.gz > database -... (5 Replies)
Discussion started by: wyclef
5 Replies

2. Red Hat

Access time of files and directories

My query please: What I saw how access times of a file and directories work. 1) For a file the access time is the time when I 1st access it after last modification of the file, i.e., if the file is modified at 10 AM and then I access it at 11 AM. After than whenever I access without... (7 Replies)
Discussion started by: ravisingh
7 Replies

3. Shell Programming and Scripting

How to list all the files, directories and sub-directories in the current path except one directory?

Can anyone come up with a unix command that lists all the files, directories and sub-directories in the current directory except a folder called log.? Thank you in advance. (7 Replies)
Discussion started by: Manjunath B
7 Replies

4. Shell Programming and Scripting

Time difference between two time stamps

Hi Friends, I have 2 varaibles which contain START=`date '+ %m/%d/%y %H:%M:%S'` END=`date '+ %m/%d/%y %H:%M:%S'` i want the time difference between the two variables in Seconds. Plz help. (2 Replies)
Discussion started by: i150371485
2 Replies

5. UNIX for Advanced & Expert Users

Help with Calculating time difference between many directories in UNIX

A report needs to come some what similar to this No of elements Stream Batch No Load time A B C D A,B,C im able to get quite easily wc -l /usr/local/intranet/areas/prod/output/SRGW_0?/*/MESSAGE_T.dat O/P of above command. A B C ... (1 Reply)
Discussion started by: peckenson
1 Replies

6. Shell Programming and Scripting

listing directories and sub directories with time and name options

Hello all! I'm looking to list directories and sub-directories of a path, on this forum I found this command: find $path -type d -exec ls -ld {} \; The issue I have is that with a simple ls, the list is listed by name, and using -t I get it by time. How could I list directories and sub... (5 Replies)
Discussion started by: nomadvisuals
5 Replies

7. UNIX for Dummies Questions & Answers

List directories and sub directories recursively excluding files

Hi, Please help me, how to get all the direcotries, its sub directories and its sub directories recursively, need to exclude all the files in the process. I wanted to disply using a unix command all the directories recursively excluding files. I tried 'ls -FR' but that display files as... (3 Replies)
Discussion started by: pointers
3 Replies

8. Shell Programming and Scripting

How to calculate time difference between start and end time of a process!

Hello All, I have a problem calculating the time difference between start and end timings...! the timings are given by 24hr format.. Start Date : 08/05/10 12:55 End Date : 08/09/10 06:50 above values are in mm/dd/yy hh:mm format. Now the thing is, 7th(08/07/10) and... (16 Replies)
Discussion started by: smarty86
16 Replies

9. Shell Programming and Scripting

Time Difference

Hi Experts... I want to calculate the time difference between two date-time values (using ksh). It can return the difference in hours (or whatever..) For eg: time_diff "09/12/2009 12:30" "09/10/2009 12:30" should return 1464 hours... $time_diff "09/12/2009 12:30:00" "09/10/2009... (5 Replies)
Discussion started by: PRKS
5 Replies
Login or Register to Ask a Question