LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Date interrogation for files created (http://www.linuxquestions.org/questions/linux-general-1/date-interrogation-for-files-created-212962/)

chunky 08-03-2004 01:22 PM

Date interrogation for files created
 
Hi,

I have a file which is called upon in one of my scripts.
Which is called fileA.

This file calls upon other files in our structure, one of these is fileB.

Is their a command in linux that will let me compare the date stamp of fileA to fileB.

Also if fileB is greater than fileA, then I action my command, if it is less than or equal to fileA, it skips to the next file?

I have manged so far to be able to isolate the file creation date, but now I am a bit stuck as to how to compare them and action my command.

# !bin/tcsh -x
set m=`ls -tr /files/| tail -1`
date +%d%m%y%T --reference='fileA'
date +%d%m%y%T--reference="/files/$m"
This gives me the output
03080409:15:24 - (FileA)
03080415:14:25 -(FileB)

As stated above, is it possible to compare these dates, and action if FileB is > than fileA.

Or is there a more simply way?

Any help much appreciated

Matt.

Muzzy 08-03-2004 02:08 PM

Probably not the sort of answer you are looking for but...

Sounds like this is exactly what make does? Perhaps use a makefile? Make defines a set of targets, the files that it depends on, and rules for creating the targets if one or more of the dependencies becomes newer than the target. It is fairly straightforward to write makefiles, syntax is:

target : dependency
[TAB]command

Plenty of websites decribing it, here's one picked from google's search results:

http://www.wlug.org.nz/MakefileHowto

Obviously this won't help you at all if you don't have make installed, in which case I am sorry!

Hope this helps,
Mark.

bigrigdriver 08-03-2004 04:31 PM

Here's a sample of code I used in a bash script I had to write for a Linux course. The user is required to enter to enter file names for comparison of several things, among others, the file creation times. $1 is the first filename entered on the command line; $2 is the second.

# which file is newer?
if test $1 -nt $2 ; then
echo "'$1' is newer than '$2'"
elif test $2 -nt $1 ; then
echo "'$2' is newer that '$1'"
else
echo "'$1' and '$2' were created at the same time."
fi

where nt means 'newer than' in bash speak.

You should be able to adapt this by replacing the echo statements with appropriate actions.


All times are GMT -5. The time now is 05:20 PM.