Unix Shell - the newer of 2 files

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > May 2007 > the newer of 2 files





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author the newer of 2 files
osiris@abydos.kmt

2007-05-27, 1:21 am

I thought of a way to compare modification dates
of two files in order to determine which is newer.
The man page does not explain this.
However, I'm not sure whether this is accurate:

$ touch old
$ touch new
$ find . -name old -newer new
<no output>
$ find . -name new -newer old
../new

I would have expected the first command to tell me
that the file named "new" is newer
yet I suspect that because "old" is not newer, there is no output.
So I should interpret this comparison in the first example as:

find (in the current directory tree) if the file with -name "old"
is -newer than the file named "new""

is that correct?
Thanks.

wfw

2007-05-27, 1:21 am

On May 27, 12:02 am, osi...@abydos.kmt wrote:
> I thought of a way to compare modification dates
> of two files in order to determine which is newer.
> The man page does not explain this.
> However, I'm not sure whether this is accurate:
>
> $ touch old
> $ touch new
> $ find . -name old -newer new
> <no output>
> $ find . -name new -newer old
> ./new
>
> I would have expected the first command to tell me
> that the file named "new" is newer
> yet I suspect that because "old" is not newer, there is no output.
> So I should interpret this comparison in the first example as:
>
> find (in the current directory tree) if the file with -name "old"
> is -newer than the file named "new""
>
> is that correct?
> Thanks.


No. The first command says

recursively descend "." and list files named 'old' AND that are newer
than 'new'.
There are none. The second swaps old and new and thus lists "new".

Jeroen van Nieuwenhuizen

2007-05-27, 1:21 am

On Sun, 27 May 2007 04:02:16 GMT
somebody claiming to be osiris@abydos.kmt wrote:
> I thought of a way to compare modification dates
> of two files in order to determine which is newer.
> The man page does not explain this.
> However, I'm not sure whether this is accurate:
>
> $ touch old
> $ touch new
> $ find . -name old -newer new
><no output>
> $ find . -name new -newer old
> ./new
>
> I would have expected the first command to tell me
> that the file named "new" is newer
> yet I suspect that because "old" is not newer, there is no output.
> So I should interpret this comparison in the first example as:
>
> find (in the current directory tree) if the file with -name "old"
> is -newer than the file named "new""
>
> is that correct?


Yes, that sounds correct to me. But you can also use the -nt or -ot option of
the test command instead of find if you want to find out which file is
newer. Something like
[ new -nt old ] && echo newer
would than output newer in your case.

Regards,

Jeroen.

--
ir. Jeroen van Nieuwenhuizen
Email: jnieuwen [at] jeroen [dot] se
I know I'm not perfect but I can smile
Daniel Rock

2007-05-27, 7:17 am

osiris@abydos.kmt wrote:
> find (in the current directory tree) if the file with -name "old"
> is -newer than the file named "new""


find operates recursively. So if in any subdirectory a file with name
"old"/"new" is found this may also be printed.

If you just want to compare the two files you can compare them
directly with find:

find old -prune -newer new
find new -prune -newer old

No need for ".". -prune is needed if old/new is a directory instead of
a regular file. See example 6 in
http://www.opengroup.org/onlinepubs...ml#tag_04_55_17

Some shells also offer the test primaries -nt/-ot. But this is a
nonstandard extension, see
http://www.opengroup.org/onlinepubs...l#tag_04_140_18

--
Daniel
Bill Marcum

2007-05-27, 1:17 pm

On Sun, 27 May 2007 04:02:16 GMT, osiris@abydos.kmt
<osiris@abydos.kmt> wrote:
>
>
> I thought of a way to compare modification dates
> of two files in order to determine which is newer.
>

Another way to compare file dates is
ls -t old new
The newest file is listed first, or last if you change -t to -tr.


--
Honk if you hate bumper stickers that say "Honk if ..."
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com