PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > the newer of 2 files
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

the newer of 2 files

Réponse
 
LinkBack Outils de la discussion
Vieux 27/05/2007, 05h02   #1
osiris@abydos.kmt
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut the newer of 2 files

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.

  Réponse avec citation
Vieux 27/05/2007, 06h45   #2
wfw
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: the newer of 2 files

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".

  Réponse avec citation
Vieux 27/05/2007, 06h45   #3
Jeroen van Nieuwenhuizen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: the newer of 2 files

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
  Réponse avec citation
Vieux 27/05/2007, 12h28   #4
Daniel Rock
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: the newer of 2 files

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/...l#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/...#tag_04_140_18

--
Daniel
  Réponse avec citation
Vieux 27/05/2007, 13h54   #5
Bill Marcum
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: the newer of 2 files

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 ..."
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h54.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10725 seconds with 13 queries