bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: a bug in "touch"?


From: Bob Proulx
Subject: Re: a bug in "touch"?
Date: Wed, 28 Jan 2004 23:26:32 -0700
User-agent: Mutt/1.3.28i

Steve Magee wrote:
> I'm running RH v9 and using the touch command
> to keep the same date and time on files after a sort.

Thanks for taking the time to report a problem.  But when reporting
problems please say what version of the program you are using.  There
are many software distributions and it is impossible to keep up with
them all.  Frequently it is found that the program isn't even one of
the ones maintained here!

  date --version

> My method used is:
> 
> touch -d "$mon $day $yrtime" filename
>
> [...]
> When issuing the command above and in the
> format above, it sets the timestamp a year ahead.
> 
> Example: In January, you issue the command above
> where $mon = Dec, $day = 31 and $yrtime = 12:30.
> These parameter are taken from the ls -la | awk command.

  touch -d "Dec 31 12:30" filename

The -d option parses a date string.  The date string is missing a year
and so the current year is assumed.  That is correct.  You can use the
'date' command to parse the string and report a full time output this
way in the current locale.

  date -d "Dec 31 12:30" 
  Fri Dec 31 12:30:00 MST 2004

Therefore touch is doing exactly as it is instructed to do.

> The file...
> -rwx------    1 major    daemon       2173 Dec 31 12:30 x
> becomes...
> -rwx------    1 major    daemon       2173 Dec 31  2004 x
> I believe this is a bug in the touch command.

I think you are missing how the 'ls' long listing format is
constructed.  It changes format depending upon the date.  Here is the
documentation on it.

  info ls
  info '(coreutils)What information is listed'

  `-l'
  `--format=long'
  `--format=verbose'
       In addition to the name of each file, print the file type,
       permissions, number of hard links, owner name, group name, size in
       bytes, and timestamp (by default, the modification time).  For
       files with a time more than six months old or in the future, the
       timestamp contains the year instead of the time of day.  If the
       timestamp contains today's date with the year rather than a time
       of day, the file's time is in the future, which means you probably
       have clock skew problems which may break programs like `make' that
       rely on file times.

Therefore ls is reporting the date in a format which has a moving
window of time around the current time.  This difference in format is
confusing your script.  It would be better to use a different time
format such as the --time-style format which can produce an
unambiguous time string.

You did not say which version of date you were using so I cannot say
if this feature is available in your version.  But I think
'ls -l --time-style=long-iso' is probably a reasonable format for your
use.

Since you said you were restoring the timestamp on the file after a
sort I suggest doing this differently.  Sort to a temporary file and
then set the timestamp on that file based on the previous file.
Perhaps something similar to this following script-let.  This is off
the top of my head and completely untested so you will have to work
out the final details yourself.  This eliminates the problems of
parsing what is meant to be human readable output from ls.

  #!/bin/sh
  trap 'rm -f $TMPFILE' 0
  TMPFILE=`mktemp -p .`
  sort myfile > $TMPFILE
  chmod --reference myfile $TMPFILE
  touch --reference myfile $TMPFILE
  mv --force $TMPFILE myfile

Bob




reply via email to

[Prev in Thread] Current Thread [Next in Thread]