The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
automatic tar xf of file with unknown name tkrahn UNIX for Dummies Questions & Answers 3 07-11-2007 07:25 AM
Automatic download of csv file using a shell script figaro Shell Programming and Scripting 1 01-13-2007 08:53 PM
File increase chapmana UNIX for Dummies Questions & Answers 7 11-24-2006 09:54 AM
Automatic Copy of File Contents to Clipboard ilak1008 Shell Programming and Scripting 5 10-10-2006 11:43 AM
unix automatic file transfer tagem UNIX for Advanced & Expert Users 4 11-14-2005 08:50 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-28-2007
steiner steiner is offline
Registered User
  
 

Join Date: Jul 2003
Location: Hambach - France
Posts: 29
Automatic name file with increase

Hello,

A file named c15a is registered on my disc all 10 minutes.

I must rename that file with the actually Date and with an automatic increase number on 5 digits, so as : c15a.20070528.00001 and the next file will be : c15a.20070528.00002.

The next day so tomorrow the five digits must be reset to 00001 so as for tomorrow : c15a.20070529.00001.

I have done the script with the rename with the date but i have a problem with the automatic increase number.

So can you help me with that increasing five digits number and automatic reset when the day changed.

So thanks a lot , bye.
  #2 (permalink)  
Old 05-28-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,355
You can try something like that :
Code:
filename=c15a

datestamp=$(date +%Y%m%d)
last_file=$(ls -1 $filename.$datestamp.* 2>/dev/null | tail -1)
if [ -z "$last_file" ]
then
  number=1
else
  number=$(( ${last_file##*.} + 1 ))
fi
new_filename=$(printf "%s.%s.%05.5d" "$filename" "$datestamp" "$number")

mv $filename $new_filename
Jean-Pierre.
  #3 (permalink)  
Old 05-28-2007
lorcan lorcan is offline
Registered User
  
 

Join Date: May 2007
Posts: 219
Deleted the statement...

Last edited by lorcan; 05-29-2007 at 12:50 AM.. Reason: Statement was wrong wrt the issue
  #4 (permalink)  
Old 05-29-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,422
Quote:
Originally Posted by lorcan
Aigles, Just a small correction in your code,
to get the latest file we have to sort based on the time,

so the 4th line should have been like

Code:
last_file=$(ls -t1 $filename.$datestamp.* 2>/dev/null | tail -1)
normally if used with tail -1, we would want ls -1tr instead. however for this particular case,i think because the filenames are already timestamped , with a number at the back, when doing ls -1, its still "sorted" to latest file.
  #5 (permalink)  
Old 05-29-2007
lorcan lorcan is offline
Registered User
  
 

Join Date: May 2007
Posts: 219
Quote:
Originally Posted by ghostdog74
normally if used with tail -1, we would want ls -1tr instead. however for this particular case,i think because the filenames are already timestamped , with a number at the back, when doing ls -1, its still "sorted" to latest file.

Yes, indeed your are correct. Plz ignore my previous msg
  #6 (permalink)  
Old 05-29-2007
steiner steiner is offline
Registered User
  
 

Join Date: Jul 2003
Location: Hambach - France
Posts: 29
Automatic increasing file

Hello ,

thanks for your quick answer.

Just a question : the little below is in C coding , isn't it ?

new_filename=$(printf "%s.%s.%05.5d" "$filename" "$datestamp" "$number")

The same line in shell script can be better because i'm not a C developper.

Ok thanks a lot bye.
  #7 (permalink)  
Old 05-29-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,422
bash also has a printf ( think its from coreutils )...anyway, type man printf, or "info printf" to see if you have it.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 07:23 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0