niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
· nixdoc.net · man pages · Linux HOWTOs · FreeBSD Tips · Forums
navigation Forum index » Programming » shell
File timestamp passed by variable?
Post new topic   Reply to topic Page 1 of 1 [7 Posts] View previous topic :: View next topic
Author Message
Dave Farrance
*nix forums Guru Wannabe


Joined: 21 Feb 2005
Posts: 294

PostPosted: Tue Jul 11, 2006 9:58 pm    Post subject: Re: File timestamp passed by variable? Reply with quote

"Chris F.A. Johnson" <cfajohnson@gmail.com> wrote:

Quote:
On 2006-07-11, Dave Farrance wrote:
"Chris F.A. Johnson" <cfajohnson@gmail.com> wrote:

fdate=$(stat -c %Y FILE)
touch -d "${fdate%.*}" FILE

Sorry, I meant:

fdate=$(stat -c %y FILE)

That works. Neat.

Quote:
OK, never mind, I've found another way around my original problem.

Which is?

Well, I'm embarrassed to admit that rebooting my PC solved the problem
of the ramdisk file that would mysteriously disappear after a few
minutes. I'd assumed that I had a bug in my script that was doing it,
but I guess that it must have been a random corruption of the ramdisk
structure.

--
Dave Farrance
Back to top
Chris F.A. Johnson
*nix forums Guru


Joined: 20 Feb 2005
Posts: 2268

PostPosted: Tue Jul 11, 2006 9:42 pm    Post subject: Re: File timestamp passed by variable? Reply with quote

On 2006-07-11, Dave Farrance wrote:
Quote:
"Chris F.A. Johnson" <cfajohnson@gmail.com> wrote:

Dave Farrance <DaveFarrance@OMiTTHiSyahooANDTHiS.co.uk> writes:

Is it possible to get the timestamp of a file with "stat", save it in a
variable, and then apply that date with "touch" to another file?

You can use stat if you like. GNU/Linux:

fdate=$(stat -c %Y FILE)
touch -d "${fdate%.*}" FILE

That gives the seconds since Epoch, but touch doesn't seem to accept
that date format.

Sorry, I meant:

fdate=$(stat -c %y FILE)

(See the stat man page.)

Quote:
OK, never mind, I've found another way around my original problem.

Which is?

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Back to top
Dave Farrance
*nix forums Guru Wannabe


Joined: 21 Feb 2005
Posts: 294

PostPosted: Tue Jul 11, 2006 8:31 pm    Post subject: Re: File timestamp passed by variable? Reply with quote

"Chris F.A. Johnson" <cfajohnson@gmail.com> wrote:

Quote:
Dave Farrance <DaveFarrance@OMiTTHiSyahooANDTHiS.co.uk> writes:

Is it possible to get the timestamp of a file with "stat", save it in a
variable, and then apply that date with "touch" to another file?

You can use stat if you like. GNU/Linux:

fdate=$(stat -c %Y FILE)
touch -d "${fdate%.*}" FILE

That gives the seconds since Epoch, but touch doesn't seem to accept
that date format.

OK, never mind, I've found another way around my original problem.

--
Dave Farrance
Back to top
Chris F.A. Johnson
*nix forums Guru


Joined: 20 Feb 2005
Posts: 2268

PostPosted: Tue Jul 11, 2006 7:00 pm    Post subject: Re: File timestamp passed by variable? Reply with quote

On 2006-07-11, Dave Farrance wrote:
Quote:
Casper H.S. Dik <Casper.Dik@Sun.COM> wrote:

Dave Farrance <DaveFarrance@OMiTTHiSyahooANDTHiS.co.uk> writes:

Is it possible to get the timestamp of a file with "stat", save it in a
variable, and then apply that date with "touch" to another file?

I've puzzled over the date formats of the two commands and there
doesn't seem to be a common format between them.

touch -r oldfile newfile

Yes, however... I'm trying to trace the reason for a repetitive disk
access, so while the test is running, I want to avoid disk accesses
from the test script. I'm using a file in a ram-disk for a time
reference for the "find" command, but due to some sort of bug, the
ramdisk file keeps disappearing. So in order to trace *that* fault, I
was hoping for a way to store a timestamp in an environment variable,
and re-apply it to a file later on. Oh well, I'll find another way
around the problem.

You can use stat if you like. GNU/Linux:

fdate=$(stat -c %Y FILE)
touch -d "${fdate%.*}" FILE

With the BSD stat it's a little more complicated, as you need to
convert seconds since the epoch.

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Back to top
Dave Farrance
*nix forums Guru Wannabe


Joined: 21 Feb 2005
Posts: 294

PostPosted: Tue Jul 11, 2006 6:36 pm    Post subject: Re: File timestamp passed by variable? Reply with quote

Casper H.S. Dik <Casper.Dik@Sun.COM> wrote:

Quote:
Dave Farrance <DaveFarrance@OMiTTHiSyahooANDTHiS.co.uk> writes:

Is it possible to get the timestamp of a file with "stat", save it in a
variable, and then apply that date with "touch" to another file?

I've puzzled over the date formats of the two commands and there
doesn't seem to be a common format between them.

touch -r oldfile newfile

Yes, however... I'm trying to trace the reason for a repetitive disk
access, so while the test is running, I want to avoid disk accesses
from the test script. I'm using a file in a ram-disk for a time
reference for the "find" command, but due to some sort of bug, the
ramdisk file keeps disappearing. So in order to trace *that* fault, I
was hoping for a way to store a timestamp in an environment variable,
and re-apply it to a file later on. Oh well, I'll find another way
around the problem.

--
Dave Farrance
Back to top
Casper H.S. Dik
*nix forums Guru


Joined: 20 Feb 2005
Posts: 1634

PostPosted: Tue Jul 11, 2006 5:44 pm    Post subject: Re: File timestamp passed by variable? Reply with quote

Dave Farrance <DaveFarrance@OMiTTHiSyahooANDTHiS.co.uk> writes:

Quote:
Is it possible to get the timestamp of a file with "stat", save it in a
variable, and then apply that date with "touch" to another file?

I've puzzled over the date formats of the two commands and there
doesn't seem to be a common format between them.

touch -r oldfile newfile

Casper
--
Expressed in this posting are my opinions. They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
Back to top
Dave Farrance
*nix forums Guru Wannabe


Joined: 21 Feb 2005
Posts: 294

PostPosted: Tue Jul 11, 2006 5:24 pm    Post subject: File timestamp passed by variable? Reply with quote

Is it possible to get the timestamp of a file with "stat", save it in a
variable, and then apply that date with "touch" to another file?

I've puzzled over the date formats of the two commands and there
doesn't seem to be a common format between them.

--
Dave Farrance
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [7 Posts] View previous topic :: View next topic
The time now is Thu Sep 09, 2010 5:45 am | All times are GMT
navigation Forum index » Programming » shell
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Reusing Postfix configuration file - main.cf ptyagi FreeBSD 0 Wed Jul 08, 2009 9:17 pm
No new posts Postfix + MySQL error: very strange variable %s iWarior Postfix 0 Mon Aug 25, 2008 2:01 pm
No new posts User Environment - export PATH variable paalepu AIX 0 Tue Sep 12, 2006 8:12 pm
No new posts Running php file everyday on scheduled time sachin PHP 1 Fri Jul 21, 2006 12:49 pm
No new posts Regarding thesaurus iso file Srikanth modules 0 Fri Jul 21, 2006 10:42 am

Copyright © 2004-2005 DeniX Solutions SRL
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum |  email marketing service
 
Sponsors: Debt Help | Breast Enlargement | Free Animated Greetings | Free Ecards | Debt Help
Privacy Policy
[ Time: 0.1288s ][ Queries: 21 (0.0950s) ][ GZIP on - Debug on ]