renaming numbered files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting renaming numbered files
# 1  
Old 11-29-2011
renaming numbered files

Hi there,

I've got a set of files that are named as follows:

image_N1_8letters.jpg
image_N2_8letters.jpg
...
image_N10_8letters.jpg
image_N11_8letters.jpg
....
image_N100_8letters.jpg
image_N101_8letters.jpg

with the "8letters" bit always consisting of 8 but always different letters. I need to rename all these files keeping the filenames as they are except that I need the "_N*_" bit as follows:

"_N001_"; "_N002_"; ...; "_N010_"; "_N011_"; .. ; "_N100_"; "_N101_"; ...

NB that a ls on my directory results in the files being listed as:

image_N100_8letters.jpg
image_N101_8letters.jpg
image_N10_8letters.jpg
...

so the files aren't listed in the correct order.

Can anyone help and give me some advice on how to write a shell script to solve that problem.

Thanks very much, your help is much appreciated.

kjartan
# 2  
Old 11-29-2011
Try:
Code:
for i in image_*; do mv $i `echo $i | perl -ne '/(image_N)(\d+)(.*)/;printf("%s%03d%s",$1,$2,$3)'`; done

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 11-29-2011
Thank you very much! It worked perfectly. Would you mind explaining it in a bit more detail (sorry but I'd like to understand it properly)?
# 4  
Old 12-02-2011
Code:
awk -F"_" '{
printf("%s_%s%03s_%s\n",$1,substr($2,1,1),substr($2,2),$3)
}' b


Last edited by Franklin52; 12-03-2011 at 11:39 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Scripting - Select multiple files from numbered list

I am trying to have the user select two files from a numbered list which will eventually be turned into a variable then combined. This is probably something simple and stupid that I am doing. clear echo "Please Select the Show interface status file" select FILE1 in *; echo "Please Select the... (3 Replies)
Discussion started by: dis0wned
3 Replies

2. Shell Programming and Scripting

Renaming multiple files in sftp server in a get files script

Hi, In sftp script to get files, I have to rename all the files which I am picking. Rename command does not work here. Is there any way to do this? I am using #!/bin/ksh For eg: sftp user@host <<EOF cd /path get *.txt rename *.txt *.txt.done ... (7 Replies)
Discussion started by: jhilmil
7 Replies

3. Shell Programming and Scripting

how to extract data from numbered files using linux in the numerical order-

Hi experts, I have a list of files containing forces as the only number as follows. Force1.txt Force2.txt Force3.txt Force4.txt Force5.txt . . . . . . . . . Force100.txt I want to put all the data(only a number ) in these forces files in the file with the same order like 1,2,3 ..100 .... (2 Replies)
Discussion started by: hamnsan
2 Replies

4. Shell Programming and Scripting

rename numbered files to numbered files with leading zeroes

Hi, I have some hundreds/thousands of files named logX.dat, where X can be any integer, and they are sequential, X ranges between 1 and any number: log1.dat log2.dat log3.dat log6.dat log10.dat ... log6000.dat I would like to rename them to scatter_params_0001.dat... (6 Replies)
Discussion started by: pau
6 Replies

5. UNIX for Dummies Questions & Answers

join files with numbered index

Hi all I´m a newbie so maybe this question will make someone mad. I am trying this command; join -a1 -11 file1 file2 > file3 file1 looks like: 1 2 3 4 5 6 7 8 9 10 11 file2: (4 Replies)
Discussion started by: awe1
4 Replies

6. Shell Programming and Scripting

renaming files or adding a name in the beginning of all files in a folder

Hi All I have a folder that contains hundreds of file with a names 3.msa 4.msa 21.msa 6.msa 345.msa 456.msa 98.msa ... ... ... I need rename each of this file by adding "core_" in the begiining of each file such as core_3.msa core_4.msa core_21.msa (4 Replies)
Discussion started by: Lucky Ali
4 Replies

7. Shell Programming and Scripting

Need help with generating m3u files in numbered directories

Hello: First, I have no idea what to search for on this task, so I'll just spell it out. I have the Bible in Audio format and each book is in a directory of it's own. 01 to 66 accordingly. I need a script to walk through each of them and ls *.mp3 > directory|book.m3u without the preceding... (2 Replies)
Discussion started by: Habitual
2 Replies

8. Shell Programming and Scripting

Listing even numbered files

Hi All, Could you please help in this case? Case: there's a directory 'CKMDB' in this directory, there are 30 files named in this manner 1.txt 2.txt 3.txt .... 30.txt Could you please guide me how to list only even numbered files like 2.txt... (5 Replies)
Discussion started by: xsam
5 Replies

9. Shell Programming and Scripting

renaming Files

Renaming Files more than 1000 in Diffrent Directories in system.. help me in this issue to resolve.... (5 Replies)
Discussion started by: sunsap
5 Replies
Login or Register to Ask a Question