[command] How to change the modification time of a file ?

  • Follow


Hello,

I would like to know how to change JUST the modification time of a file ?

I tried the 'touch' command but I have to specify the date as well.

How can I do ?
And I would like to avoid to extract the date of the file for put it again.

Thanks.

--
Bc.


0
Reply Bc 8/4/2004 12:52:31 PM

2004-08-4, 13:52(+01), Bc.:
> I would like to know how to change JUST the modification time of a file ?
>
> I tried the 'touch' command but I have to specify the date as well.
>
> How can I do ?
> And I would like to avoid to extract the date of the file for put it again.
[...]

touch won't let you do that.

You'll have to extract the date from the file. And compute the
new date.

Try:

hour=12
min=34
sec=56
file=/some/file

perl -e 'use POSIX;
         @s = stat($ARGV[0]);
         @t = localtime($s[9]); $t[0]=0;
         @t[0..2] = @ARGV[1..3];
         utime($s[8], mktime(@t), $ARGV[0]);
        ' "$file" "$sec" "$min" "$hour"

-- 
Stephane
0
Reply Stephane 8/4/2004 1:41:44 PM


Bc. wrote:

> I would like to know how to change JUST the modification time of a file ?
> I tried the 'touch' command but I have to specify the date as well.

"man touch" tells you how to specify the date/time.
0
Reply Oscar 8/4/2004 2:31:19 PM

Bc. wrote:

> And I would like to avoid to extract the date of the file for put it again.

Do you want to extract the date/time?  (avoid?)

Use touch with -r to "copy" the date/time of the file on a temp file,
then touch -r again from the temp file to the original file.

man touch

  -r ref_file
         Uses the corresponding times  of  the  file  named  by
         ref_file instead of the current time.
0
Reply Oscar 8/4/2004 2:36:58 PM

> > And I would like to avoid to extract the date of the file for put it
again.
>
> Do you want to extract the date/time?  (avoid?)

If I can't just update the time, I have to extract the date of the file.

> Use touch with -r to "copy" the date/time of the file on a temp file,
> then touch -r again from the temp file to the original file.
>
> man touch
>
>   -r ref_file
>          Uses the corresponding times  of  the  file  named  by
>          ref_file instead of the current time.

The problem is I want to update the time without changing the date.
For instance, I have a file :

-rw-------   1 root     other         50 Aug  4 14:37 /tmp/config.cfg

and I want to change the time to 01:00, and keep the same date.

-rw-------   1 root     other         50 Aug  4 01:00 /tmp/config.cfg

With the option -r, it doesn't work, because I can't change the time and
keep the date. It changes the both.
And the problem is the same because I have to update the time on the temp
file.....

--
Bc.


0
Reply Bc 8/4/2004 3:01:03 PM

> You'll have to extract the date from the file. And compute the
> new date.
>
> Try:
>
> hour=12
> min=34
> sec=56
> file=/some/file
>
> perl -e 'use POSIX;
>          @s = stat($ARGV[0]);
>          @t = localtime($s[9]); $t[0]=0;
>          @t[0..2] = @ARGV[1..3];
>          utime($s[8], mktime(@t), $ARGV[0]);
>         ' "$file" "$sec" "$min" "$hour"

I really don't want use perl....
May is it possible in shell script ?

--
Bc.


0
Reply Bc 8/4/2004 3:02:46 PM

In article <ceqtli$k3g$1@zcars0v6.ca.nortel.com>,
 "Bc." <moimoi.toi2@laposte.net> wrote:

> > You'll have to extract the date from the file. And compute the
> > new date.
> >
> > Try:
> >
> > hour=12
> > min=34
> > sec=56
> > file=/some/file
> >
> > perl -e 'use POSIX;
> >          @s = stat($ARGV[0]);
> >          @t = localtime($s[9]); $t[0]=0;
> >          @t[0..2] = @ARGV[1..3];
> >          utime($s[8], mktime(@t), $ARGV[0]);
> >         ' "$file" "$sec" "$min" "$hour"
> 
> I really don't want use perl....
> May is it possible in shell script ?

That's up to you to decide if it's worth your time to write a shell 
script that parses the file's mtime, removes the date component (ls does 
display it if the file was created less than 6 months ago), and performs 
the update.  IMO, Perl is your quickest, easiest option.

You'll have to each your vegetables if you want dessert, young man.  Go 
ahead.  They're good for you.

-- 
DeeDee, don't press that button!  DeeDee!  NO!  Dee...



0
Reply Michael 8/4/2004 4:52:19 PM

2004-08-4, 16:02(+01), Bc.:
[...]
>> hour=12
>> min=34
>> sec=56
>> file=/some/file
>>
>> perl -e 'use POSIX;
>>          @s = stat($ARGV[0]);
>>          @t = localtime($s[9]); $t[0]=0;
>>          @t[0..2] = @ARGV[1..3];
>>          utime($s[8], mktime(@t), $ARGV[0]);
>>         ' "$file" "$sec" "$min" "$hour"
>
> I really don't want use perl....
> May is it possible in shell script ?
[...]

Sure. It would be inefficient, illegible, not reliable, and
maybe not portable to another system:

#! /bin/sh -
# usage: xxx <file> HHMM[.SS]
file=$1 hms=$2

months="JanFebMarAprMayJunJulAugSepOctNovDec"
set -f

set x `LC_ALL=C ls -l -- "$file"`
eval "`LC_ALL=C date +'cur_year=%Y cur_month=%m'`"
month=`expr "$months" : ".*$7" / 3`
case $9 in
  *:*)
    year=`expr "$cur_year" - \( "$month" \> "$cur_month" \)`;;
  *)
    year=$9
esac
case $month in
  ?) month=0$month;;
esac
case $8 in
  ?) day=0$8;;
  *) day=$8;;
esac
LC_ALL=C touch -t "$year$month$day$hms" -- "$file"


-- 
Stephane
0
Reply Stephane 8/5/2004 11:29:47 AM

> > I really don't want use perl....
> > May is it possible in shell script ?
> [...]
>
> Sure. It would be inefficient, illegible, not reliable, and
> maybe not portable to another system:
>
> #! /bin/sh -
> # usage: xxx <file> HHMM[.SS]
> file=$1 hms=$2
>
> months="JanFebMarAprMayJunJulAugSepOctNovDec"
> set -f
>
> set x `LC_ALL=C ls -l -- "$file"`
> eval "`LC_ALL=C date +'cur_year=%Y cur_month=%m'`"
> month=`expr "$months" : ".*$7" / 3`
> case $9 in
>   *:*)
>     year=`expr "$cur_year" - \( "$month" \> "$cur_month" \)`;;
>   *)
>     year=$9
> esac
> case $month in
>   ?) month=0$month;;
> esac
> case $8 in
>   ?) day=0$8;;
>   *) day=$8;;
> esac
> LC_ALL=C touch -t "$year$month$day$hms" -- "$file"

yep, it looks a bit heavy....

I think I found a good solution to my (specific) problem, because I have to
change the time just after I modify the files, and the time it's just some
minutes before.
So the date is the same as the current date, and I can do :

 touch `date '+%m%d'`0100 /tmp/config.cfg

Thanks all for yours answers.

--
Bc.


0
Reply Bc 8/5/2004 11:45:18 AM

Stephane CHAZELAS <this.address@is.invalid> wrote:
> Sure. It would be inefficient, illegible, not reliable, and
> maybe not portable to another system:

Not much better in any respect, but I like the tar hack as a stat
replacement, and /usr/bin/printf:

  #! /bin/sh -
  # usage: xxx <file> HHMM[.SS]
  file=$1 hms=$2

  months="JanFebMarAprMayJunJulAugSepOctNovDec"
  set -f

  set x `LC_ALL=C tar cf - "$file" | tar tvf - 2>/dev/null`

  month=`expr "$months" : ".*$5" / 3`
  month=`printf "%02d" $month`
  day=`printf "%02d" $6`
  year=$8

  LC_ALL=C touch -t "$year$month$day$hms" -- "$file"

mp.
-- 
Systems Administrator | Institute for Software Science | Univ. of Vienna
0
Reply Martin 8/5/2004 1:51:07 PM

9 Replies
957 Views

(page loaded in 0.164 seconds)

Similiar Articles:













7/25/2012 10:57:54 AM


Reply: