How to change file's timestamp?

Peter Gordon peter at thecodergeek.com
Tue Aug 22 16:06:12 UTC 2006


On Tue, 22 Aug 2006 10:56:11 -0500 (CDT), Hongwei Li wrote:
> The problem is when I run it at command line, it works, but not in   
> the script:
>
> # touch --date="Tue Aug 22 08:30:00 CDT 2006" t1
> works well. However, the script mystmp:
>
> #!/bin/sh
> newstmp=$1
> touch --date=$newstmp t1
>
> and run mystmp as:
> # ./mystmp "Tue Aug 22 08:30:00 CDT 2006"
>
> didn't do what I want, but created:
>
> # ls -l
> total 32
> -rw-r--r-- 1 root root   0 Aug 22 00:00 08:30:00
> -rw-r--r-- 1 root root   0 Aug 22 00:00 2006
> -rw-r--r-- 1 root root   0 Aug 22 00:00 22
> -rw-r--r-- 1 root root   0 Aug 22 00:00 Aug
> -rw-r--r-- 1 root root   0 Aug 22 00:00 CDT
> -rwxr-xr-x 1 root root 420 Aug 22 10:47 mystmp
> -rw-r--r-- 1 root root   0 Aug 22 00:00 t1
>
> What's wrong? Thanks!

The problem is that the parameter has spaces within it, so when you  
use that as
a variable within your script, you need to enclose it with quotes. Your script
currently expands that command to something such as:

	touch --date=Tue Aug 22 08:30:00 CDT 2006 t1

which - since the "Aug," "22," "08:30:00," etc files don't exist -  
creates them
and sets their timestamps to the current time. You need to use --date="$1" so
that the command is interpreted as you had intended:

	touch --date="$1" foo.file

That would expand to something such as:

	touch --date="Tue Aug 22 08:30:00 CDT 2006" foo.file

which is what I believe you are intending to do.

Hope that helps.
-- 
Peter Gordon (codergeek42)
This message was sent through a webmail interface,
     thus has no digital signature.




More information about the fedora-list mailing list