dbaspot
Tags Register FAQ Calendar Search Today's Posts Mark Forums Read

comparing two files last modification date - shell

This is a discussion on comparing two files last modification date - shell ; On Nov 13, 11:45*am, Stephane Chazelas wrote: > 2008-11-13, 17:12(+01), Janis Papanagnou:> puzzlecracker wrote: > > >> Thanks the following doesnt quite do what I want. First I want to see > >> if file exist and if it does, ...


Home > Database Forum > Operating Systems > shell > comparing two files last modification date

Reply

 

LinkBack Thread Tools Display Modes
  #11  
Old 11-13-2008, 02:31 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: comparing two files last modification date

On Nov 13, 11:45*am, Stephane Chazelas
wrote:
> 2008-11-13, 17:12(+01), Janis Papanagnou:> puzzlecracker wrote:
>
> >> Thanks the following doesnt quite do what I want. First I want to see
> >> if file exist and if it does, I want to compare

>
> >> if * -e $obj || * find $i *-prune -newer $obj -print | grep -q .;

>
> > if [[ -e $obj ]] || ...

>
> > Or use single [...] instead of [[...]] in case you use an old shell.

>
> [...]
>
> Or in case you want to write a standard script that can run with
> any POSIX compliant shell, which in general I'd recommend.
>
> [ -e "$obj" ] or ls -Ld -- "$obj" > /dev/null 2>&1
>
> Note that [ -e will return false for a symlink to a non-existent
> file (or a file in a directory you don't have access to).
>
> The equivalent of
> ls -d -- "$obj" > /dev/null 2>&1 would be:
> [ -e "$obj" ] || [ -L "$obj" ].
>
> Another note on symlinks.
>
> find x -newer y and ls -t x y will compare the dates of x and y
> even if x or y is a symlink.
>
> While ls -tL x y, find -H x -newer y and [[ x -nt y ]] will
> compare the dates of the targets of the symlinks.
>
> --
> Stéphane


Stéphane I don't see how this finds date of two files. Please explain.
Here is what I want to do:

I want perform some action only if:
1) file2 does exist
or
2) file2 was created before, and hence older than file1


Thanks
Reply With Quote
  #12  
Old 11-13-2008, 03:20 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: comparing two files last modification date

2008-11-13, 10:31(-08), puzzlecracker:
[...]
>> >> if * -e $obj || * find $i *-prune -newer $obj -print | grep -q . ;

[...]
> I want perform some action only if:
> 1) file2 does exist
> or
> 2) file2 was created before, and hence older than file1

[...]

Your requirement does not make sense to me.

Maybe you mean

1) file2 does NOT exit
or
2) file2 exists and is older than file1.

if [ ! -e file2 ] ||
find file1 -prune -newer file2 | grep -q .
then ...; fi

or

if ! find file2 -prune ! -older file1 2> /dev/null | grep -q .

Maybe you'd want to consider "make" for that instead:

file2: file1
action

In a Makefile.

--
Stéphane
Reply With Quote
  #13  
Old 11-13-2008, 05:19 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: comparing two files last modification date

On Nov 13, 2:20*pm, Stephane CHAZELAS
wrote:
> 2008-11-13, 10:31(-08), puzzlecracker:
> [...]>> >> if * -e $obj || * find $i *-prune -newer $obj -print | grep -q . ;
> [...]
> > I want perform some action only if:
> > 1) file2 does exist
> > * * *or
> > 2) file2 was created before, and hence older than file1

>
> [...]
>
> Your requirement does not make sense to me.


How so? All I want to do is to do "something" ONLY is file2 doesn't
exist or file2 is older than file1. In other words, say I have
Foo.cpp and Foo files, latter being a binary. Now, I want to recompile
and build Foo object only if it doesn't exist or Foo.cpp has been
modified.

Let me know if you still unsure of requirements..


> Maybe you mean
>
> 1) file2 does NOT exit
> * or
> 2) file2 exists and is older than file1.
>
> if [ ! -e file2 ] ||
> * find file1 -prune -newer file2 | grep -q .
> then ...; fi
>
> or
>
> if ! find file2 -prune ! -older file1 2> /dev/null | grep -q .
>
> Maybe you'd want to consider "make" for that instead:
>
> file2: file1
> * * * * action
>
> In a Makefile.
>
> --
> Stéphane


In fact, I am writing a script similar to make. However, all of my
programs/projects are one file long, I don't need makes overhead. Yes,
otherwise I would use make for this purpose.


Reply With Quote
  #14  
Old 11-13-2008, 05:27 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: comparing two files last modification date

On Nov 13, 4:19*pm, puzzlecracker wrote:
> On Nov 13, 2:20*pm, Stephane CHAZELAS
> wrote:
>
> > 2008-11-13, 10:31(-08), puzzlecracker:
> > [...]>> >> if * -e $obj || * find $i *-prune -newer $obj -print |grep -q . ;
> > [...]
> > > I want perform some action only if:
> > > 1) file2 does exist
> > > * * *or
> > > 2) file2 was created before, and hence older than file1

>
> > [...]

>
> > Your requirement does not make sense to me.

>
> How so? All *I want to do is to do "something" ONLY is file2 doesn't
> exist or file2 is older than file1. *In other words, *say I have
> Foo.cpp and Foo files, latter being a binary. Now, I want to recompile
> and build Foo object only if it doesn't exist or Foo.cpp has been
> modified.
>
> Let me know if you still unsure of requirements..
>
>
>
> > Maybe you mean

>
> > 1) file2 does NOT exit
> > * or
> > 2) file2 exists and is older than file1.

>
> > if [ ! -e file2 ] ||
> > * find file1 -prune -newer file2 | grep -q .
> > then ...; fi

>
> > or

>
> > if ! find file2 -prune ! -older file1 2> /dev/null | grep -q .

>
> > Maybe you'd want to consider "make" for that instead:

>
> > file2: file1
> > * * * * action

>
> > In a Makefile.

>
> > --
> > Stéphane

>
> In fact, I am writing a script similar to make. However, all of my
> programs/projects are one file long, I don't need makes overhead. Yes,
> otherwise I would use make for this purpose.


Also, I get this error with that condition:

line 21: syntax error near unexpected token `then'
line 21: ` find $i -prune -newer $obj | grep -q . ;
then'
Reply With Quote
  #15  
Old 11-13-2008, 05:36 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: comparing two files last modification date

puzzlecracker wrote:
> On Nov 13, 2:20 pm, Stephane CHAZELAS
> wrote:
>
>>2008-11-13, 10:31(-08), puzzlecracker:
>>[...]>> >> if -e $obj || find $i -prune -newer $obj -print | grep -q . ;
>>[...]
>>
>>>I want perform some action only if:
>>>1) file2 does exist
>>> or
>>>2) file2 was created before, and hence older than file1

>>
>>[...]
>>
>>Your requirement does not make sense to me.

>
>
> How so? All I want to do is to do "something" ONLY is file2 doesn't
> exist or file2 is older than file1. In other words, say I have
> Foo.cpp and Foo files, latter being a binary. Now, I want to recompile
> and build Foo object only if it doesn't exist or Foo.cpp has been
> modified.


I'm too lazy to look that up for any other shell, but in kornshell, if
you use [[ file1 -nt file2 ]] or [[ file1 -ot file2 ]] (i.e. newer than
or older than, resp.) the semantics is

true if file1 is newer than file2 or file2 does not exist

and similar for "-ot", "older than". Use negations and switch the roles
of file1 and file2 to cover any case you need.

(Sometimes it's simpler to switch to a modern shell than stick to old
restricted features.)

Janis

>
> Let me know if you still unsure of requirements..
>
>
>
>>Maybe you mean
>>
>>1) file2 does NOT exit
>> or
>>2) file2 exists and is older than file1.
>>
>>if [ ! -e file2 ] ||
>> find file1 -prune -newer file2 | grep -q .
>>then ...; fi
>>
>>or
>>
>>if ! find file2 -prune ! -older file1 2> /dev/null | grep -q .
>>
>>Maybe you'd want to consider "make" for that instead:
>>
>>file2: file1
>> action
>>
>>In a Makefile.
>>
>>--
>>Stéphane

>
>
> In fact, I am writing a script similar to make. However, all of my
> programs/projects are one file long, I don't need makes overhead. Yes,
> otherwise I would use make for this purpose.
>
>

Reply With Quote
  #16  
Old 02-23-2010, 02:45 AM
Database Expert
 
Join Date: Feb 2010
Posts: 42
HENARRY JAYA is on a distinguished road
Default Re: comparing two files last modification date

You could get some activities outside the home for yourself. It's hard to feel lonely if you're nice and busy. I don't know what he does for a living but perhaps you could talk to him and see if he could find a way to have at least one day of the week that he can commit to spending the evening with you.
It could be your "date" night and it would give you something to look forward to and plan for every week. Best of luck to you.
Reply With Quote
Reply

Thread Tools
Display Modes



All times are GMT -4. The time now is 01:05 PM.