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 ; I have two files, say file1 and file2, I would like to find out which of these two files was modified last. Thanks...


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

Reply

 

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

I have two files, say file1 and file2, I would like to find out which
of these two files was modified last.

Thanks
Reply With Quote
  #2  
Old 11-13-2008, 10:49 AM
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:
> I have two files, say file1 and file2, I would like to find out which
> of these two files was modified last.


You can inspect that roughly with ls -l
If you want it with higher precision have a look at the command stat.
For visual inspection with user friendly dates use

stat -x%y file1 file2

and if you want to do arithmetic use the output values (seconds) from

stat -x%Y file1 file2


Janis

>
> Thanks

Reply With Quote
  #3  
Old 11-13-2008, 11:13 AM
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, 9:49*am, Janis Papanagnou
wrote:
> puzzlecracker wrote:
> > I have two files, say file1 and file2, I would like to find out which
> > of these two files was modified last.

>
> You can inspect that roughly with ls -l
> If you want it with higher precision have a look at the command stat.
> For visual inspection with user friendly dates use
>
> * *stat -x%y file1 file2
>
> and if you want to do arithmetic use the output values (seconds) from
>
> * *stat -x%Y file1 file2
>
> Janis
>
>
>
> > Thanks


argh stat: invalid option -- x
Reply With Quote
  #4  
Old 11-13-2008, 11:18 AM
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, 10:13*am, puzzlecracker wrote:
> On Nov 13, 9:49*am, Janis Papanagnou
> wrote:
>
>
>
> > puzzlecracker wrote:
> > > I have two files, say file1 and file2, I would like to find out which
> > > of these two files was modified last.

>
> > You can inspect that roughly with ls -l
> > If you want it with higher precision have a look at the command stat.
> > For visual inspection with user friendly dates use

>
> > * *stat -x%y file1 file2

>
> > and if you want to do arithmetic use the output values (seconds) from

>
> > * *stat -x%Y file1 file2

>
> > Janis

>
> > > Thanks

>
> argh stat: invalid option -- x


Try "man stat" on your system (e.g. on an Ubuntu system here stat has
a "-c" option instead of a "-x" option).
Reply With Quote
  #5  
Old 11-13-2008, 11:28 AM
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, 06:35(-08), puzzlecracker:
> I have two files, say file1 and file2, I would like to find out which
> of these two files was modified last.

[...]

Some shells (ksh, zsh, bash) have [[ file1 -nt file2 ]].

In the standard toolchest, you've got "find -newer" and "ls -t"

if find file1 -prune -newer file2 -print | grep -q .; then
echo "file1 is newer than file2"
fi

Or:

NL='
'
case $(ls -td file1 file2) in
("file1${NL}file2") echo "file1 is not older than file2";;
("file2${NL}file1") echo "file2 is not older than file1";;
(*) echo >&2 error;;
esac


--
Stéphane
Reply With Quote
  #6  
Old 11-13-2008, 11:33 AM
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, 9:49 am, Janis Papanagnou
> wrote:
>
>>puzzlecracker wrote:
>>
>>>I have two files, say file1 and file2, I would like to find out which
>>>of these two files was modified last.

>>
>>You can inspect that roughly with ls -l
>>If you want it with higher precision have a look at the command stat.
>>For visual inspection with user friendly dates use
>>
>> stat -x%y file1 file2
>>
>>and if you want to do arithmetic use the output values (seconds) from
>>
>> stat -x%Y file1 file2
>>
>>Janis
>>
>>
>>
>>
>>>Thanks

>
>
> argh stat: invalid option -- x


Sorry, mistyped! I meant -c (x and c are adjacent on my keyboard).

Janis
Reply With Quote
  #7  
Old 11-13-2008, 11:51 AM
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, 10:28*am, Stephane CHAZELAS
wrote:
> 2008-11-13, 06:35(-08), puzzlecracker:> I have two files, say file1 and file2, I would like to find out which
> > of these two files was modified last.

>
> [...]
>
> Some shells (ksh, zsh, bash) have [[ file1 -nt file2 ]].
>
> In the standard toolchest, you've got "find -newer" and "ls -t"
>
> if find file1 -prune -newer file2 -print | grep -q .; then
> * echo "file1 is newer than file2"
> fi
>
> Or:
>
> NL='
> '
> case $(ls -td file1 file2) in
> * ("file1${NL}file2") echo "file1 is not older than file2";;
> * ("file2${NL}file1") echo "file2 is not older than file1";;
> * (*) echo >&2 error;;
> esac
>
> --
> Stéphane

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 . ;
then


fi


Thanks
Reply With Quote
  #8  
Old 11-13-2008, 12:12 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:
>
> 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.

Janis

> then
>
>
> fi
>
>
> Thanks

Reply With Quote
  #9  
Old 11-13-2008, 12: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

2008-11-13, 07:51(-08), puzzlecracker:
[...]
> 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 . ;
> then
>
>
> fi

[...]

-e is not a valid command. The command that does tests is the
"[" command (aka test). "||" means OR, you want "&&" for AND.
You've forgotten the quotes around the variable expansions. But
you don't need to run the "[" command anyway, you can as well do:

if find "$i" -prune -newer "$obj" -print 2> /dev/null | grep -q .
then ...
fi

find will not output anything on its standard output if it can't
find "$i", so the test will fail.

Note that the above will fail to work properly if "$i" starts
with a "-".

--
Stéphane
Reply With Quote
  #10  
Old 11-13-2008, 12:45 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, 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
Reply With Quote
Reply

Thread Tools
Display Modes



All times are GMT -4. The time now is 10:39 PM.