Automatic name file with increase


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Automatic name file with increase
# 1  
Old 05-28-2007
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  
Old 05-28-2007
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  
Old 05-29-2007
Deleted the statement...

Last edited by lorcan; 05-29-2007 at 01:50 AM.. Reason: Statement was wrong wrt the issue
# 4  
Old 05-29-2007
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  
Old 05-29-2007
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  
Old 05-29-2007
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  
Old 05-29-2007
bash also has a printf ( think its from coreutils )...anyway, type man printf, or "info printf" to see if you have it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Automatic file movement between folders

Hello There, Here is the use cases Input folders dropbox/project/abc/ dropbox/project/pqr/ dropbox/project/lmn/ dropbox/project/xyz/ Target Folders /data/abc/ /data/pqr/ /data/lmn/ /data/xyz/ (2 Replies)
Discussion started by: TreasureCookies
2 Replies

2. Shell Programming and Scripting

Create a specific bash to increase the automatic update on my pi

hi everyone, I just began to be interested about the bash access. I buy a time ago a Raspberry pi, I installed raspbmc and now I would like build a bash to copy everyday all new files inside my server directly on the hard drive in my Pi. So my HDD is directly plug on my pi with usb connect,... (4 Replies)
Discussion started by: nagito34
4 Replies

3. UNIX for Advanced & Expert Users

Automatic File Patching

Hi All, I have a query regarding the automatic patching on files in unix Suppose I have a file names - abc.txt and I take its backup as abc.txt_bkp and then do some changes in abc.txt and save it as abc.txt_new. Now I want to automatically apply the changes done on abc.txt to... (0 Replies)
Discussion started by: Shubham Aggarwa
0 Replies

4. Solaris

how to increase file size in solaris 10 os

hi, let me know how to increase file size in solaris 10 OS (4 Replies)
Discussion started by: meet2muneer
4 Replies

5. Shell Programming and Scripting

Automatic file input to code

i have got many files like this in my folder temp(say) imp_02042008.txt for date 02-04-2008 imp_03092009.txt for date 03-09-2009 imp_25112009.txt for date 25-11-2009 ................... ........ in some folder. and one of my shell code uses one of the above files based on date.... (9 Replies)
Discussion started by: Maruti
9 Replies

6. Shell Programming and Scripting

[code] Automatic File Input

i have got many files like this in my folder temp(say) imp_02042008.txt for date 02-04-2008 imp_03092009.txt for date 03-09-2009 imp_25112009.txt for date 25-11-2009 ................... ........ in some folder. and one of my shell code uses one of the above files based on date.... (0 Replies)
Discussion started by: Maruti
0 Replies

7. Solaris

Script for automatic count the file i have

Hi I have a folder will all the file ftp in everyday. I have to go in every morning to check to see everynight have all the 2000+ file have been all FTP in. so every morning i will check like this # ls -l *_20080904*_20080904* |wc if there is 2714 that mean is correct. File name:... (13 Replies)
Discussion started by: summerpeh
13 Replies

8. UNIX for Dummies Questions & Answers

automatic tar xf of file with unknown name

Hi all, With curl I can fetch a tar archive from a web server which contains a file ending with .scf which I am interested in. Unfortunately the file name may vary and the subdirectory inside the tar archive may change. I can manually browse the directory structure and extract the file and then... (3 Replies)
Discussion started by: tkrahn
3 Replies

9. UNIX for Dummies Questions & Answers

File increase

Sorry im really new here this is my second post today! My question is, im trying to write a script and i want to output to a text file but i want each text file to be different so for instance log.txt, log1.txt, log2.txt ect how would i do that? (7 Replies)
Discussion started by: chapmana
7 Replies

10. UNIX for Advanced & Expert Users

unix automatic file transfer

Hello, I am a beginner with korn shell scripting. I have got a text file that gets produced every night and I need to transfer it to a windows shared area. Is there any command line script (e.g FTP) that I could use to transfer the file automatically without manual intervention everyday? Any... (4 Replies)
Discussion started by: tagem
4 Replies
Login or Register to Ask a Question