rename numbered files to numbered files with leading zeroes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting rename numbered files to numbered files with leading zeroes
# 1  
Old 11-17-2011
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 scatter_params_0002.dat scatter_params_0003.dat scatter_params_0006.dat scatter_params_0010.dat scatter_params_6000.dat

Note that in the scatter files I need them with leading zeroes, which are absent in the original log files.

Any help will be appreciated.

thanks

Pau
# 2  
Old 11-17-2011
Code:
#!/bin/ksh93
for ((i=1;i<6000;i++))
do
   mv $(printf "log%d.dat" $i) $(printf "scatter_params_%04d.dat" $i)
done

Replace #!/bin/ksh93 by #!/bin/bash if missing.
# 3  
Old 11-17-2011
if all your file names are in the file pau.txt:
Code:
log1.dat
log2.dat
log3.dat
log6.dat
log10.dat
log6000.dat

Code:
nawk -F'.' '{a=$1;sub("[^0-9]+", "",a);printf("mv %s  scatter_params_%04d.dat\n", $0, a)}' pau.txt

once satisfied with the results pipe the above to 'sh':
Code:
nawk -F'.' '{a=$1;sub("[^0-9]+", "",a);printf("mv %s  scatter_params_%04d.dat\n", $0, a)}' pau.txt | sh

# 4  
Old 11-17-2011
thanks!!!

The first reply runs over 6000 every time, which is not very convenient

The second answer workds just fine... but since this is going to be part of a script, I am wondering about the sh part of it... can it be removed?

Thanks again
# 5  
Old 11-17-2011
Quote:
Originally Posted by pau
The first reply runs over 6000 every time, which is not very convenient
Smilie Isn't it what you asked for ?
Do you simply want the number of files to process to be passed as a parameter ? or to stop when there is no source file to rename ?
# 6  
Old 11-17-2011
Quote:
Originally Posted by pau
thanks!!!

The first reply runs over 6000 every time, which is not very convenient

The second answer workds just fine... but since this is going to be part of a script, I am wondering about the sh part of it... can it be removed?

Thanks again
Hmmm... I'm confused. You the trailing '|sh' to do the actual renaming.
I provided the part withOUT the '|sh' for you to test/troubleshoot the 'mv' command. Once this is done, you'd need to add the trailing '|sh' to do the renaming.
Maybe I'm missing something obvious - I've been known to do that.
# 7  
Old 11-17-2011
Code:
for file in log*.dat;do mv $file  $(printf "scatter_parrams_%04d.dat" ${file//[a-z.]/});done

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

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

3. Shell Programming and Scripting

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... (3 Replies)
Discussion started by: kjartan
3 Replies

4. 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

5. 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

6. 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

7. Shell Programming and Scripting

delete first number of even-numbered lines

Hello ! I am trying to delete a number from the even-numbered lines of a pipeline after having extracted and sorted the desired data from an original text file using sed... sed -r 's/\(*\)*\((*)\),*,*,(*)*: * \w*\=(*).*/\3,\1/g' | sort -n then the data looks like : Now, I am... (3 Replies)
Discussion started by: ShellBeginner
3 Replies

8. Shell Programming and Scripting

loop through numbered filenames

Hi I'm very new to this script thing, so please be gentle. I am trying to get a command - the mach2qtl command in the code below - to loop through a set of files. Each command should take the same two .dat and .ped files, but the .mlinfo and .mlprob files with filenames including 'chrom1' ... (7 Replies)
Discussion started by: polly_falconer
7 Replies

9. UNIX for Dummies Questions & Answers

Setting numbered variables

Normally I would post in the shell scripting area, but this is so basic I thought I'd best put it in the dummy area! I want to set a series of numbered variables. I have a loop which increments a variable called $i with each loop. I want to name variables with this number e.g. var1, var2, var3... (1 Reply)
Discussion started by: michaeltravisuk
1 Replies
Login or Register to Ask a Question