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 08-03-2003, 02:32 PM   #1
tslinux
LQ Newbie
 
Registered: Aug 2003
Posts: 8

Rep: Reputation: 0
bash script - incrementing a filename in a script


what is the best way to increment a filename in a bash script so that the filename is different each time the script is run and file is saved? ie. filename+1, filename+2, etc. example used in tar backup.
 
Old 08-03-2003, 04:43 PM   #2
slapNUT
Member
 
Registered: Jun 2001
Location: Recycle Bin
Distribution: Linux & Everything else on VirtualBox
Posts: 144

Rep: Reputation: 15
filename` date|awk 'OFS="."{print $2,$3,$6,$4}'`
Makes a filename like this:
filenameAug.3.2003.16:46:02
 
Old 08-03-2003, 09:54 PM   #3
tslinux
LQ Newbie
 
Registered: Aug 2003
Posts: 8

Original Poster
Rep: Reputation: 0
Wink Response

Thanks, dude (or dudette?)

I really appreciate it.
And, I really do plan on learning this stuff.

Thanks.
 
Old 08-03-2003, 11:05 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Just a suggestion ... if you replace
date
in the script with
date +%Y%m%d-%H%M%S
you save yourself the awk'ing
and get a more easily (logically)
sortable set of filenames... and save
yourself the ":" in filenames, which
scp, for instance, doesn't like :}

Cheers,
Tink
 
Old 08-04-2003, 11:06 PM   #5
slapNUT
Member
 
Registered: Jun 2001
Location: Recycle Bin
Distribution: Linux & Everything else on VirtualBox
Posts: 144

Rep: Reputation: 15
Yeah Tinksters is better. Except for that "weird European, puttin the year first" stuff. :P

Maybe:
date +%m%d%Y-%H%M%S
 
Old 08-05-2003, 11:45 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
That's not European.
In Europe (well, at least in The Netherlands) we use dd-mm-yyyy.

However, when sorting alphabetically yyyy-mm-dd is nicer I think.

Last edited by Hko; 08-05-2003 at 11:48 AM.
 
Old 08-05-2003, 04:01 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally posted by slapNUT
Yeah Tinksters is better. Except for that "weird European, puttin the year first" stuff. :P

Maybe:
date +%m%d%Y-%H%M%S
date +%Y%m%d-%H%M%S
That's not European, that's logical :} ... think
about it for a second. Once you have a date
range of more than a year it's stupid to sort
by months first. "My way" is also the way dates
are represented internally in all major SQL
databases, too.

Cheers,
Tink

Last edited by Tinkster; 08-05-2003 at 04:05 PM.
 
Old 08-05-2003, 07:28 PM   #8
slapNUT
Member
 
Registered: Jun 2001
Location: Recycle Bin
Distribution: Linux & Everything else on VirtualBox
Posts: 144

Rep: Reputation: 15
It was a joke!

But since you brought it up; you don't have to put the year first in the file name to sort by the year.
Code:
From man sort:
       -k, --key=POS1[,POS2]
              start a key at POS1, end it at POS 2 (origin 1)
That lets you sort filenames starting from any position you choose.
 
Old 08-05-2003, 07:33 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Yes, you could ... or you could stick with the
logical, straightforward approach and then use
sort if you need something different ;)

Cheers,
Tink
 
Old 08-05-2003, 08:06 PM   #10
tslinux
LQ Newbie
 
Registered: Aug 2003
Posts: 8

Original Poster
Rep: Reputation: 0
you guys can end this, now.
 
Old 08-05-2003, 11:58 PM   #11
slapNUT
Member
 
Registered: Jun 2001
Location: Recycle Bin
Distribution: Linux & Everything else on VirtualBox
Posts: 144

Rep: Reputation: 15
Quote:
Posted by Tinkster
Yes, you could ...
Thank you benevolent <cough> guru. I'm done.
 
  


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 script to remove capitalisation and spaces form a filename scuzzman Programming 11 05-18-2008 12:28 PM
Getting the first part of a filename in a BASH script trevelluk Programming 3 02-15-2005 01:06 AM
Bash script to change a filename associated with an inode index number. Ziv Programming 22 06-19-2004 08:41 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM

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

All times are GMT -5. The time now is 03:43 AM.

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