LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-08-2007, 07:05 PM   #1
espiesior
LQ Newbie
 
Registered: Apr 2007
Posts: 5

Rep: Reputation: 0
BASH: the dateandtimestamp on files


Can any BASH scripter here help me?

I need to rename a file by its date and time of creation (up to seconds). What is the best method of doing this with BASH script.

The format of the date and time (to seconds) in the filename could be anything that a human can recognize.


example

datetimeoffilecreation = getdatetimeofcreation( file )
mv file datetimeoffilecreation

example
/home/file
/home/08-04-07-21-04-59 #DD-MM-YY-HH-MM-SS
 
Old 04-08-2007, 08:08 PM   #2
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by espiesior
Can any BASH scripter here help me?

I need to rename a file by its date and time of creation (up to seconds). What is the best method of doing this with BASH script.

You usually cannot get the time of a file's creation; it is not stored on most UNIX systems. What you can get is the time of the last modification.

Quote:
The format of the date and time (to seconds) in the filename could be anything that a human can recognize.

example

datetimeoffilecreation = getdatetimeofcreation( file )
mv file datetimeoffilecreation

example
/home/file
/home/08-04-07-21-04-59 #DD-MM-YY-HH-MM-SS

If you have GNU date (which is standard in GNU/Linuz systems and may be installed as gdate on some other systems) you can generate the date of the file in whatever format you choose (I recommend a slightly modified ISO format, as in this example):

Code:
filetime=$( date -r "$file" +%Y-%m-%d_%H.%M.%s )
mv "$file" "$filetime"
 
Old 04-08-2007, 09:07 PM   #3
espiesior
LQ Newbie
 
Registered: Apr 2007
Posts: 5

Original Poster
Rep: Reputation: 0
Thank you very much! It all works except for the seconds and I got that to work by changing %s to %S.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Files HLA91 Linux - Newbie 3 10-19-2006 08:30 PM
Bash files > array tigeraven Programming 3 12-13-2005 03:40 AM
Generate files using bash kaon Linux - General 1 03-08-2005 01:59 PM
making .tif files into animated .gif files (bash shell, Red Hat 7.2) illiniguy3043 Linux - Newbie 1 06-01-2004 04:04 PM
bash files ChimpFace9000 Linux - General 1 08-23-2002 09:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration