LinuxQuestions.org
Help answer threads with 0 replies.
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 07-23-2010, 06:21 AM   #1
tensigh
Member
 
Registered: Mar 2004
Location: Tokyo, Japan
Distribution: Backtrack 5 R3
Posts: 145

Rep: Reputation: 15
date formatting from stat command


Hello,

I'm writing a script that takes the date attribute of a file when it was created and appending it to the name. I'm pretty close to what I need, but the date appears in yyyy-mm-dd format and I want it to read mm-dd-yyyy. What I have done so far is something like

for ((i=1;i<=50;i++));
do fdate=`stat -c %y Video\ Snapshot\ $i.png|awk {'print$1'}`;
newname=Personname-$fdate-$i.png;
mv Video\ Snapshot\ $i.png $newname;
done

This works decently (I know it's not pretty - if someone has a better idea I'm all open!) but the date comes out in the yyyy-mm-dd format. How do I change it?

Thanks in advance for your help!
 
Old 07-23-2010, 06:30 AM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
1. make stat give you the time in seconds since the epoch
2. use awk + strftime to convert

Code:
 stat -c %Y Video\ Snapshot\ $i.png | awk '{print strftime("%m-%d-%Y", $1)}'
 
Old 07-23-2010, 06:31 AM   #3
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 542

Rep: Reputation: 79
Try this:
Code:
for ((i=1;i<=50;i++));
do fdate=`date --date=@$( stat -c%Y Video\ Snapshot\ $i.png|awk {'print$1'} )`;
newname=Personname-$fdate-$i.png;
mv Video\ Snapshot\ $i.png $newname;
done
 
Old 07-23-2010, 06:53 AM   #4
tensigh
Member
 
Registered: Mar 2004
Location: Tokyo, Japan
Distribution: Backtrack 5 R3
Posts: 145

Original Poster
Rep: Reputation: 15
Thanks. That sort of worked

Quote:
Originally Posted by angel115 View Post
Try this:
Code:
for ((i=1;i<=50;i++));
do fdate=`date --date=@$( stat -c%Y Video\ Snapshot\ $i.png|awk {'print$1'} )`;
newname=Personname-$fdate-$i.png;
mv Video\ Snapshot\ $i.png $newname;
done
Thanks for the code. The new file name has today's date appended to it not the day the file was created. This is close but still not what I'm aiming for.

Let me back up, maybe there's an easier way of doing this.

I take snapshots of my family members when I talk to them on Skype (I live overseas and my sister's kids are still young) and they get called "Video Snapshot x.png". I manually go through and use a for loop to change the date and rename the files.

For example;
for ((i=1;i<=49;i++));do mv Video\ Snapshot $i.png Family-7-19-2010-$i.png; done

then for the next day's pics

x=0; for ((i=50;i<=76;i++));do let x=x+1;mv Video\ Snapshot $i.png Family-7-23-2010-$x.png; done

What I'd like is a script that reads the date attribute and changes the filenames so they say something like Family-pics-mm-dd-yyyy-<number>.png without my manually going through the directory and figuring which numbers correspond to which dates. It's not a pain to do it manually but I thought it would be a good way to practice programming.
 
Old 07-23-2010, 07:41 AM   #5
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
It is impossible to get the date of creation. That information is not stored by any kernel or file system. (You could potentially pull a hack by reading the EXIF data of your images..)


You used %y yourself which is date of last modification.

man stat:
Code:
       %y     Time of last modification
       %Y     Time of last modification as seconds since Epoch

Last edited by AlucardZero; 07-23-2010 at 07:43 AM.
 
Old 07-23-2010, 10:53 AM   #6
tensigh
Member
 
Registered: Mar 2004
Location: Tokyo, Japan
Distribution: Backtrack 5 R3
Posts: 145

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by AlucardZero View Post
It is impossible to get the date of creation. That information is not stored by any kernel or file system. (You could potentially pull a hack by reading the EXIF data of your images..)


You used %y yourself which is date of last modification.

man stat:
Code:
       %y     Time of last modification
       %Y     Time of last modification as seconds since Epoch
Sorry, I meant to say that the date they're modified is their date of creation. Now are there any other hairs to split here......

Incidentally, the question of how to swap the date goes unanswered. Any clues?
 
Old 07-23-2010, 10:57 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
you can use find's printf to format your new file name

Code:
$ find file -printf "%f:%Am-%Ad-%Ay"
file:07-23-10
 
Old 08-29-2010, 07:06 PM   #8
Bruce Hill
HCL Maintainer
 
Registered: Jun 2003
Location: McCalla, AL, USA
Distribution: Arch, Gentoo
Posts: 6,940

Rep: Reputation: 129Reputation: 129
Quote:
Originally Posted by kuriharu View Post
I take snapshots of my family members when I talk to them on Skype (I live overseas and my sister's kids are still young) and they get called "Video Snapshot x.png". I manually go through and use a for loop to change the date and rename the files.
I'm trying to find out how to make Skype take a snapshot in Linux.

No problems in the darkside, but can't find that option in Linux.

Using skype_static-2.1.0.81 with 32-bit Slackware.

Edit: noticed after a further read that the op must be taking snapshots in Windoze

Last edited by Bruce Hill; 08-29-2010 at 07:19 PM.
 
Old 08-29-2010, 08:50 PM   #9
tensigh
Member
 
Registered: Mar 2004
Location: Tokyo, Japan
Distribution: Backtrack 5 R3
Posts: 145

Original Poster
Rep: Reputation: 15
Yup, I have to

Yeah, I'm using Windows. Skype in Linux is just beta and the Windows version just runs a lot better. The other thing is that my wireless connection conks out on me in Linux, too. Never does that in Windows.

I hate to say it, but virtually all of my desktop functions in Linux are "beta", it's almost like desktop Linux is beta itself. I hate saying that, I really do, but I have all types of minor annoyances CONSTANTLY in desktop Linux that I haven't dealt with since Win98.

If I have one more video driver problem I might just ditch desktop Linux altogether. It's 2010 and I STILL deal with video driver problems in Linux! Dare I say it? Windows is more stable than ..... No, I just can't bring myself to say it, but these little ankle biter problems in desktop Linux are really getting to me (no problems with it as a server platform, not one).

Sorry to rant, frustrated as he** today....
 
Old 08-29-2010, 11:35 PM   #10
Bruce Hill
HCL Maintainer
 
Registered: Jun 2003
Location: McCalla, AL, USA
Distribution: Arch, Gentoo
Posts: 6,940

Rep: Reputation: 129Reputation: 129
Thanks for your reply ... only fanbois don't accept obvious reality.
Having to boot into Windoze to use Adobe InDesign and Photoshop
give me a great new appreciation and desire to get back into Linux.

I fully understand. At least Windows 7 has a better DE than the past ones;
and is a better OS all around than previous Mickey$oft offerings.

Skype, and QQ, beta for Linux are horrible, and haven't been updated in ions.
Can't blame them, as they must pay programmers to code for that. This shows
that the real hackers in *nix aren't playing games and doing a lot of desktop
stuff ... just look how horrible KDE is, and all the K* apps. It's not quite
achieved Win98 stability, but they keep adding graphic failure eye candy.

Being from Japan you'll like the fact that Linus approved of Win 7.
 
Old 08-30-2010, 04:13 AM   #11
tensigh
Member
 
Registered: Mar 2004
Location: Tokyo, Japan
Distribution: Backtrack 5 R3
Posts: 145

Original Poster
Rep: Reputation: 15
Love the pic

The pic with Linus is classic; not just because it looks like he's thumbs-upping Win7 but I've never seen him smile that big before!!

There are things I hate about Windows, too, of course (if my OS tells me one more damn time I can't disconnect my drive because it's being used when it's not....), but overall it just works better in my experience.

On the same computer, my Ubuntu partitions requires that I re-set my video resolution about 1 in 4 boots. I don't think I've ever had to do that on my Windows partition. And it pisses me off to no end to upgrade and suddenly programs stop working! Because the new version of Ubuntu uses lib-qmid.vtrx.1.03.0875 instead of lib-qmid.vtrx.1.03.873 suddenly a screen saver or other utility I love won't work. That's lame! WTF!!!

Don't get me wrong, I still love using Linux when it works. But I must say I spend more time configuring and tweaking Linux, whereas I spend more time actually using my computer more in Windows. It's refreshing how much work you get done when you don't have to chase down library files, old .deb files because apt-get repositories have been "updated" to remove your old, working programs, etc.

Sorry, again, venting frustration here.
 
  


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
using stat to determine last file access and comparing to current date in IF statemnt danpaluska Linux - Newbie 2 02-16-2010 11:52 AM
stat command ravidangaych Linux - General 3 09-22-2009 05:02 AM
PHP: formatting current date & time as: 2006-04-12 13:47:36 and substract 15 minutes NaCo Linux - Software 4 11-13-2008 07:31 PM
formatting date output grob115 Linux - Newbie 2 01-28-2006 08:03 AM
the format of the date in stat in wrong rhuser Programming 9 03-16-2003 12:46 PM

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

All times are GMT -5. The time now is 02:00 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