Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Could you tell me how to get timestamp of created date of file and rename the file name with it by JavaScript, Perl or shell on OSX 10.9.2?

Example:

Existing: Untitled.txt timestamp is 2014/05/03 01:01:01

New: %prefix%_Untitled_20140503_0101.txt (seconds is not necessary)

Reason:

I'm having strange things that the timestamp is changed looks by microwave/V2K technology frequently. But I have to know when it's created. because these are evidence of the matter. Please kindly help.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Get the creation time of the file use stat command. The output will be in seconds (since unix epoch).

  • -n option suppresses the newline at end of output.
  • -f permits use of "printf" formatting. %B is the creation or "birth" date of the file.

    D=$(stat -nf'%B' your_file)
    

Then use date -r option to input date in seconds. The +%Y... aargument specifies the date format.

   DF=$(date -r $D '+%Y%m%d_%H%M')

The bash scripting would be something like this-:

 export PREFIX="your_prefix"
 D=$(stat -nf'%B' your_file)            # seconds since creation time of file
 DF=$(date -r $D '+%Y%m%d_%H%M')             # formatted creation date/time
 mv your_file "${PREFIX}_your_file_${DF}.txt"  # rename the file
share|improve this answer
    
Thank you for your answer but What I would like to know how to read the time stamp of file and rename the file name with time stamp. sorry about my English. –  Juza May 5 at 5:13
    
No worries - sorry about the confusion. I've changed the answer to suit your requirements. –  suspectus May 5 at 9:11
    
Thank you very much! it worked! It's very helpful! –  Juza May 6 at 7:36
    
No problem - I'm happy you have a solution. –  suspectus May 6 at 8:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.