Script to change file names


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to change file names
# 1  
Old 09-15-2015
Script to change file names

I have a landing directory on my unix (solaris) server, that receives the following files:

Code:
MLH4301I AAOT-hhslog.610.20150805.txt
MLH4301I AAOT-hhslog.611.20150805.txt
MLH4301I AAOT-hhslog.612.20150805.txt
MLH4301I AAOT-hhslog.613.20150805.txt

and I need to add to this files the number 10000 (ten thousand) before the sequence (610, 611, 612, 613 and so on)
To become like this:

Code:
MLH4301I AAOT-hhslog.10000610.20150805.txt
MLH4301I AAOT-hhslog.10000611.20150805.txt
MLH4301I AAOT-hhslog.10000612.20150805.txt

Please can you help me in achieving this
# 2  
Old 09-15-2015
Hello fretagi,

Following may help you in same.
Code:
ls -lhtr AAOT-hhslog* | awk '{A=$NF;sub("AAOT-hhslog.","AAOT-hhslog.10000",$0);print "mv " A OFS $NF}'

Output will be as follows.
Code:
mv AAOT-hhslog.613.20150805.txt AAOT-hhslog.10000613.20150805.txt
mv AAOT-hhslog.612.20150805.txt AAOT-hhslog.10000612.20150805.txt
mv AAOT-hhslog.611.20150805.txt AAOT-hhslog.10000611.20150805.txt
mv AAOT-hhslog.610.20150805.txt AAOT-hhslog.10000610.20150805.txt

If you are happy with code and results above then you can use following.
Code:
ls -lhtr AAOT-hhslog* | awk '{A=$NF;sub("AAOT-hhslog.","AAOT-hhslog.10000",$0);print "mv " A OFS $NF}' | sh

Thanks,
R. Singh
# 3  
Old 09-15-2015
Hi Singh

Thank you very much, I will try now, but will it work for thousands of files?
# 4  
Old 09-15-2015
Hello fretagi,

It depends on the ARG_MAX value of your os(BASH in my case) else you will get Argument list too long
error if it crosses that limit. You can check that limit by as follows.
Code:
cat /usr/include/linux/limits.h
#define ARG_MAX       131072    /* # bytes of args + environ for exec() */
 
getconf ARG_MAX
131072

Now to avoid that problem you can do as follows.
Code:
for i in AAOT-hhslog.*
do
 echo $i | awk '{A=$NF;sub("AAOT-hhslog.","AAOT-hhslog.10000",$0);print "mv " A OFS $NF}'
done

If you are happy with above code's result then use following.
Code:
for i in AAOT-hhslog.*
do
 echo $i | awk '{A=$NF;sub("AAOT-hhslog.","AAOT-hhslog.10000",$0);print "mv " A OFS $NF}' | sh
done

Thanks,
R. Singh
# 5  
Old 09-15-2015
Hi Singh!

my operating system is actually solaris 10 and I am using
Code:
 echo $SHELL
/sbin/sh

But can you explain that last piece of code (your last script) as I am still a begginer in writing scripts
# 6  
Old 09-15-2015
Hello fretagi,

Following may help you in same.
Code:
for i in AAOT-hhslog.*                                                                             ####### It will look for files starting named with AAOT-hhslog and so on.
do
 echo $i | awk '{A=$NF;sub("AAOT-hhslog.","AAOT-hhslog.10000",$0);print "mv " A OFS $NF}'   ######## now using awk to get the move command ready.
                                                                                            ######## Mentioning variable A to $NF, now subtituting word "AAOT-hhslog. to "AAOT-hhslog.10000 in each line, then printing mv A(which is the previous file_name) $0(new file name as per your requirement.)
done

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 7  
Old 09-15-2015
Try
Code:
for i in MLH*; do echo "mv '$i' '${i/log./log.10000}'"; done
mv 'MLH4301I AAOT-hhslog.610.20150805.txt' 'MLH4301I AAOT-hhslog.10000610.20150805.txt'
mv 'MLH4301I AAOT-hhslog.611.20150805.txt' 'MLH4301I AAOT-hhslog.10000611.20150805.txt'
mv 'MLH4301I AAOT-hhslog.612.20150805.txt' 'MLH4301I AAOT-hhslog.10000612.20150805.txt'
mv 'MLH4301I AAOT-hhslog.613.20150805.txt' 'MLH4301I AAOT-hhslog.10000613.20150805.txt'

Remove echo when happy.
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Exclude certain file names while selectingData files coming in different names in a file name called

Data files coming in different names in a file name called process.txt. 1. shipments_yyyymmdd.gz 2 Order_yyyymmdd.gz 3. Invoice_yyyymmdd.gz 4. globalorder_yyyymmdd.gz The process needs to discard all the below files and only process two of the 4 file names available ... (1 Reply)
Discussion started by: dsravanam
1 Replies

2. Shell Programming and Scripting

Change the file name and copy old file content to new file names.

Hi, I have a files in a directory as below :- ls -1 mqdepth-S1STC02 proc-mq-S1STC01 proc-mq-S1STC02 proc-mq-S1STC03 Whereever i have S1STC i need to copy them into new file with file name S2STC. expected output :- ls -1 mqdepth-S2STC02 proc-mq-S2STC01 proc-mq-S2STC02... (3 Replies)
Discussion started by: satishmallidi
3 Replies

3. Shell Programming and Scripting

Challenge to change file names

Hi, How can I change following file name in a bash script? From file names: myfile-module-1.0-3.0.el6.x86_64.package To file names: myfile-module1_0-1.0-3.0.el6.x86_64.package ^ ^ ^ ^ ^ ^ ^ ^ Basically, the digit 1.0 is a version number, the digit 3.0 is... (11 Replies)
Discussion started by: hce
11 Replies

4. UNIX for Dummies Questions & Answers

Change sequence names in fasta file

I have fasta files with multiple sequences in each. I need to change the sequence name headers from: >accD:_59176-60699 ATGGAAAAGTGGAGGATTTATTCGTTTCAGAAGGAGTTCGAACGCA >atpA_(reverse_strand):_showing_revcomp_of_10525-12048 ATGGTAACCATTCAAGCCGACGAAATTAGTAATCTTATCCGGGAAC... (2 Replies)
Discussion started by: tyrianthinae
2 Replies

5. Red Hat

How to change name to get rid of name in front of file names?

admin.campaign.sql admin.cardnumber_filter.sql understand that rename is using mv command but how do I rename such that it become the following: campaign.sql cardnumber_filter.sql thanks (2 Replies)
Discussion started by: jediwannabe
2 Replies

6. Shell Programming and Scripting

Change unique file names into new unique filenames

I have 84 files with the following names splitseqs.1, spliseqs.2 etc. and I want to change the .number to a unique filename. E.g. change splitseqs.1 into splitseqs.7114_1#24 and change spliseqs.2 into splitseqs.7067_2#4 So all the current file names are unique, so are the new file names.... (1 Reply)
Discussion started by: avonm
1 Replies

7. Shell Programming and Scripting

change multiple file names

Hi is it possible to change multiple files (~10k) names with out disturbing the data in it. ? input Hynda|cgr10(+):100027702-1000312480|.txt Hynda|cgr10(+):100027702-1000312483|.txt Hynda|cgr10(+):100027702-1000312484|.txt Hynda|cgr10(+):100027702-1000312482|.txt output... (4 Replies)
Discussion started by: quincyjones
4 Replies

8. Shell Programming and Scripting

Change multiple file names

Hello, I have some files in a directory like: 01_07_2010_aa.txt 01_07_2010_bb.txt 01_07_2010_cc.txt 01_07_2010_dd.txt 01_07_2010_ee.txt 01_07_2010_ff.txt I want to change their names to : 3nm_aa.txt 3nm_bb.txt 3nm_cc.txt 3nm_dd.txt 3nm_ee.txt 3nm_ff.txt (8 Replies)
Discussion started by: ad23
8 Replies

9. UNIX for Dummies Questions & Answers

Change All File Names in a Directory

Hi, If I have a directory full of say 100 random files, and I would like to organize them, for example: FILE001, FILE002, FILE003, FILE004, etc. How would I do this from Terminal, instead of manually changing each file? I'm using Mac OS X, if that makes a difference. Thank you in advance... (8 Replies)
Discussion started by: andou
8 Replies

10. Shell Programming and Scripting

How to change automatically the file names

Hi all, I need to replace automatically all special characters of one filename with some corresponding characters For example > ö --> oe ä --> ae .... If the special character comes more than one time, then all the coccuerences have to be replaced. I would like to have a... (6 Replies)
Discussion started by: MAKY
6 Replies
Login or Register to Ask a Question