The time now is 09/05/08 - 01:05
Log in: Username: Password:
Search forums for:
  

Inserting a datestamp in a filename under linux?

Post new topic   Reply to topic
Author Message
Jukas
Toomuchtimeonhands
Toomuchtimeonhands


Joined: 19 Mar 2003
Posts: 896



PostPosted: 04/08/06 - 13:06    Post subject: Inserting a datestamp in a filename under linux? Reply with quote

I have several sql databases that I want to dump on a daily basis and backup. No problem, I can setup a simple script/cron job to do it. But I can't for the life of me figure out how to rename a file to include the current datestamp. I know how to do it via a batchfile in Windoze but I can't figure out how to do it via the command line.

For example taking filepoopy.tar.gz and renaming it to filepoopy04082006.tar.gz .

Ideally it would rename the file and then copy the renamed file to a different directory/network share.


Oh, and did I mention I'm lazy and don't know perl?
Back to top
motherface
RealPoor Guru
RealPoor Guru


Joined: 12 Mar 2003
Posts: 3407



PostPosted: 04/08/06 - 15:16    Post subject: Reply with quote

$ date '+%Y%m%d';

$ for i in `/bin/ls`; do d=`date '+%Y%m%d'`; mv $i $i$d; done
Back to top
motherface
RealPoor Guru
RealPoor Guru


Joined: 12 Mar 2003
Posts: 3407



PostPosted: 04/08/06 - 15:17    Post subject: Reply with quote

Also, always put the most significant digit furthest to the left, e.g. YMD rather than MDY or DMY. That way they sort properly.
Back to top
Occulis
RealPoor Jedi
RealPoor Jedi


Joined: 11 Oct 2002
Posts: 13293
Location: Moral Relativity Central



PostPosted: 04/08/06 - 15:20    Post subject: Reply with quote

I have a 1-line script called 'arcname'. Could do it with an alias but, whatever:

Code:

#!/bin/sh
date +%Y.%b.%d_at_%H.%M


Then if i want to use it I go:


Code:


occ@demon:~> echo "Boy this is wonderful." >> `arcname`-thoughts.txt
occ@demon:~> cat 2006.Apr.08_at_14.08-thoughts.txt
Boy this is wonderful.

Back to top
motherface
RealPoor Guru
RealPoor Guru


Joined: 12 Mar 2003
Posts: 3407



PostPosted: 04/08/06 - 16:03    Post subject: Reply with quote

You could also maybe pipe the file to rotatelogs or cronolog if the app supports piping.
Back to top
Lumberg
RealPoor Guru
RealPoor Guru


Joined: 17 Nov 2002
Posts: 4404
Location: i'm in ur base, i'm killin ur dudes



PostPosted: 04/08/06 - 17:31    Post subject: Reply with quote

yeah
Back to top
Occulis
RealPoor Jedi
RealPoor Jedi


Joined: 11 Oct 2002
Posts: 13293
Location: Moral Relativity Central



PostPosted: 04/08/06 - 17:35    Post subject: Reply with quote

lumberg, i hate aerosmith, please change your avatar
Back to top
motherface
RealPoor Guru
RealPoor Guru


Joined: 12 Mar 2003
Posts: 3407



PostPosted: 04/08/06 - 18:54    Post subject: Reply with quote

Occulis wrote:
lumberg, i hate aerosmith, please change your avatar
lawlz
Back to top
Lumberg
RealPoor Guru
RealPoor Guru


Joined: 17 Nov 2002
Posts: 4404
Location: i'm in ur base, i'm killin ur dudes



PostPosted: 04/08/06 - 23:58    Post subject: Reply with quote

Occulis wrote:
lumberg, i hate aerosmith, please change your avatar


find me one, baldy

Back to top
Display posts from previous:   
Post new topic   Reply to topic
Page 1 of 1

Related topics:
Post owned for inserting my AIM name