[SOLVED] Rename multiple files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [SOLVED] Rename multiple files
# 1  
Old 12-04-2012
[SOLVED] Rename multiple files

Hi. I have a large number of files with names like:

t_ 0.20000E-02.dat

There is actually a space after the underscore. These files are numbered numerically, i.e. t_ 0.20000E-02.dat, t_ 0.21000E-02.dat, t_ 0.22000E-02.dat and so on.

What I would like to do is rename such that the file with the lowest numerical value becomes t1.dat, and the one next to it becomes t2.dat and so on.

I think what I have to do is find the number of files in the directory and sort them according to their numerical value. Then I have to do a loop over them to rename them accordingly. But this seems to be quite involved to me. Any ideas?

Thanks!
# 2  
Old 12-04-2012
Code:
$ ls -1 | sort -k 1.4 | while read a; do i=$((i+1));echo mv "$a" t${i}.dat; done
mv t_ 0.20000E-02.dat t1.dat
mv t_ 0.21000E-02.dat t2.dat
mv t_ 0.22000E-02.dat t3.dat

if you are happy with the move command, then remove the echo and run it
This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 12-04-2012
Thanks for the reply. The problem I have is I also have files such as t_ 0.10000E-03.dat, t_ 0.20000E-03.dat etc, which are not sorted properly, so I get the following:

Code:
mv t_  0.0000    .dat t1.dat
mv t_ 0.10000E-02.dat t2.dat
mv t_ 0.10000E-03.dat t3.dat
mv t_ 0.11000E-02.dat t4.dat
mv t_ 0.12000E-02.dat t5.dat

# 4  
Old 12-04-2012
try this: This will remove all the spaces and replace it with an underscore and then rename them

Code:
ls -1 "t_ "* | while read filename ; do flnm=`echo "$filename"  | tr -s " " "_"`; cp "$filename" $flnm; done

use cp command just in case.
now run this code to rename them t1,t2 etc

Code:
i=0;for file in `ls -1 t_*`; do i=$((i+1)); mv "$filen" t${i}.dat; done

Please run it with echo statements, to see if that is what you want.
You can club the loops together once you are sure what each thing is doing.

Last edited by grep_me; 12-04-2012 at 05:48 PM.. Reason: Removed Echo statements
This User Gave Thanks to grep_me For This Post:
# 5  
Old 12-04-2012
That is a useless use of ls * and useless use of backticks. You do not need ls to use *, that's the shell's job. You're only getting the list of files out of ls when you use * because you're cramming the list of files into ls in the first place.

Furthermore, you don't need to cram everything on one line. ; can be replaced by a new line for better readability in nearly all cases.

Code:
i=1

for FILE in "t_ "*
do
        echo mv "$FILE" t${i}.dat
        let i=i+1
done

Remove the echo once you've tested and are sure it does what you want.
This User Gave Thanks to Corona688 For This Post:
# 6  
Old 12-05-2012
Hi, I'm afraid the codes are still doing this:

Code:
mv t_0.0000_.dat t1.dat
mv t_0.10000E-02.dat t2.dat
mv t_0.10000E-03.dat t3.dat

Whereas, I was hoping it would sort them according to their numerical ascending order then rename them as:

Code:
mv t_0.0000_.dat t1.dat
mv t_0.10000E-03.dat t2.dat
mv t_0.20000E-03.dat t3.dat

Perhaps it may help that I know the change in the numerical value between the files is 0.1E-3, i.e. it is i=i+0.1E-3

But thanks for the code that removes the space character!
# 7  
Old 12-05-2012
Well, the way they're sorted, they don't really sort numerically. No sort command I can think of is going to grok floating point numbers in exponential notation.

So the usual trick is to rename them into something that will sort.

Are there any positive exponents or only negative ones? Any negative numbers or only positive ones? What do they look like?

Last edited by Corona688; 12-05-2012 at 11:33 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SBATCH trinity for multiple files and rename/move the output files

Hey guys, I have wrote the following script to apply a module named "trinity" on my files. (it takes two input files and spit a trinity.fasta as output) #!/bin/bash -l #SBATCH -p node #SBATCH -A <projectID> #SBATCH -n 16 #SBATCH -t 7-00:00:00 #SBATCH --mem=128GB #SBATCH --mail-type=ALL... (1 Reply)
Discussion started by: @man
1 Replies

2. Shell Programming and Scripting

Rename a multiple files

I have multiple files in folder which i want to rename. hence I am using the below command in my script by I get an error: export XXX_LOG_DIR="${LOG_DIR}/${XXX_HOST}/xxx/${REPORT_DATE}" mv $XXX_LOG_DIR/*.audit.gz $XXX_LOG_DIR/*.audit.log.gz But I get the below error: mv: target... (5 Replies)
Discussion started by: karan8810
5 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Search, Extract and Rename Multiple Files

Hi, I want perl script for the below requirement. We have lot of files like below name in the directory 750464921-RE-file2.csv 750452173-RE-file1.csv 750385426-RE-file3.csv 750373470-RE-file4.csv And also we have another file as "Group.csv" in the same directory as per the below format... (9 Replies)
Discussion started by: armsaran
9 Replies

4. UNIX for Dummies Questions & Answers

Rename Multiple Files

Hey guys, I am the definition of a newbie. I am in the process of trying to rip all my dvds onto a new HTPC I setup. While doing this, I am also trying to organize a bunch of other files I already have to proper naming conventions. So far I have just been naming each file separately (I am on a... (4 Replies)
Discussion started by: Ralze34
4 Replies

5. Shell Programming and Scripting

Rename multiple files

hello: I have multiple files with names like: somestring_y2010m01d01 somestring_y2010m01d02 .......... somestring_y2010m12d31 How... (4 Replies)
Discussion started by: sylcam
4 Replies

6. Shell Programming and Scripting

Rename the multiple files

Hi I need to reanme the multiple file using unix script I have multiple file like: sample_YYYYMMDD.xls test new_YYYYMMDD.xls simple_YYYYMMDD.xls I need to rename this file sample.xls testnew.xls SIMPLE.xls thanks (8 Replies)
Discussion started by: murari83.ds
8 Replies

7. UNIX for Dummies Questions & Answers

help with multiple files rename...

Hi everyone, I'm very green in Linux. Please help me to solve my problem. I have thousands of files and I want to change their names. They have naming convection: prefix_date_date+1_suffix.nc prefix: ext-GLORY date_date+1: 20020101_20020102 and two types of suffix: gridV_R20020130 and... (3 Replies)
Discussion started by: makikicindy
3 Replies

8. UNIX for Dummies Questions & Answers

How to rename multiple files

Hi all, I have some files like: pickup.0000043200.t001.t001.data pickup.0000043200.t001.t002.data pickup.0000043200.t002.t001.data pickup.0000043200.t002.t002.data pickup.0000043200.t003.t001.data pickup.0000043200.t003.t002.data I need to rename these files to ... (4 Replies)
Discussion started by: a_dor8
4 Replies

9. Shell Programming and Scripting

rename multiple files

Hi all, I have some files like: pickup.0000043200.t001.t001.data pickup.0000043200.t001.t002.data pickup.0000043200.t002.t001.data pickup.0000043200.t002.t002.data pickup.0000043200.t003.t001.data pickup.0000043200.t003.t002.data I need to rename these files to ... (3 Replies)
Discussion started by: a_dor8
3 Replies

10. UNIX for Dummies Questions & Answers

Rename multiple files

Hello, I want to rename multiple files at a time and I don't know how to do it. I have various ".mp3" files, like "band name - music name.mp3" and I want to remove the "band name" from all files. Anybody knows how to do it using shell script or sed or even perl? Thanks (7 Replies)
Discussion started by: luiz_fer10
7 Replies
Login or Register to Ask a Question