[SOLVED] Rename multiple files | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

[SOLVED] Rename multiple files

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-04-2012
lost.identity lost.identity is offline
Registered User
 
Join Date: Oct 2009
Last Activity: 28 October 2014, 10:09 AM EDT
Posts: 62
Thanks: 49
Thanked 1 Time in 1 Post
[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!
Sponsored Links
    #2  
Old 12-04-2012
itkamaraj's Avatar
itkamaraj itkamaraj is offline Forum Advisor  
^Kamaraj^
 
Join Date: Apr 2010
Last Activity: 14 November 2014, 7:56 AM EST
Posts: 3,059
Thanks: 33
Thanked 659 Times in 638 Posts

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
The Following User Says Thank You to itkamaraj For This Useful Post:
lost.identity (12-04-2012)
Sponsored Links
    #3  
Old 12-04-2012
lost.identity lost.identity is offline
Registered User
 
Join Date: Oct 2009
Last Activity: 28 October 2014, 10:09 AM EDT
Posts: 62
Thanks: 49
Thanked 1 Time in 1 Post
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
grep_me grep_me is offline
Registered User
 
Join Date: Oct 2008
Last Activity: 16 December 2014, 11:03 AM EST
Location: CT
Posts: 46
Thanks: 8
Thanked 3 Times in 3 Posts
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
The Following User Says Thank You to grep_me For This Useful Post:
lost.identity (12-05-2012)
Sponsored Links
    #5  
Old 12-04-2012
Corona688 Corona688 is online now Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 19 December 2014, 4:36 PM EST
Location: Saskatchewan
Posts: 20,030
Thanks: 855
Thanked 3,454 Times in 3,226 Posts
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.
The Following User Says Thank You to Corona688 For This Useful Post:
lost.identity (12-05-2012)
Sponsored Links
    #6  
Old 12-05-2012
lost.identity lost.identity is offline
Registered User
 
Join Date: Oct 2009
Last Activity: 28 October 2014, 10:09 AM EDT
Posts: 62
Thanks: 49
Thanked 1 Time in 1 Post
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!
Sponsored Links
    #7  
Old 12-05-2012
Corona688 Corona688 is online now Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 19 December 2014, 4:36 PM EST
Location: Saskatchewan
Posts: 20,030
Thanks: 855
Thanked 3,454 Times in 3,226 Posts
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..
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Rename the multiple files murari83.ds Shell Programming and Scripting 8 08-01-2011 04:35 PM
help with multiple files rename... makikicindy UNIX for Dummies Questions & Answers 3 01-12-2010 03:51 AM
How to rename multiple files a_dor8 UNIX for Dummies Questions & Answers 4 04-13-2009 09:47 AM
rename multiple files a_dor8 Shell Programming and Scripting 3 04-12-2009 09:33 PM
rename multiple files antointoronto Shell Programming and Scripting 13 03-20-2008 11:16 AM



All times are GMT -4. The time now is 05:38 PM.