LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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, 03: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, 05: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, 10: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-04-2003, 12:05 AM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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-05-2003, 12:06 AM   #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, 12:45 PM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
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 12:48 PM.
 
Old 08-05-2003, 05:01 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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 05:05 PM.
 
Old 08-05-2003, 08: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, 08:33 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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, 09:06 PM   #10
tslinux
LQ Newbie
 
Registered: Aug 2003
Posts: 8

Original Poster
Rep: Reputation: 0
you guys can end this, now.
 
Old 08-06-2003, 12:58 AM   #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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


All times are GMT -5. The time now is 04:13 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration