|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
[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
|
||||
|
||||
|
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.datif 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
|
|||
|
|||
|
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
|
|||
|
|||
|
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; donePlease 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 04: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
|
|||
|
|||
|
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
doneRemove 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
|
|||
|
|||
|
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
|
|||
|
|||
|
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 10:33 AM.. |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 03:35 PM |
| help with multiple files rename... | makikicindy | UNIX for Dummies Questions & Answers | 3 | 01-12-2010 02:51 AM |
| How to rename multiple files | a_dor8 | UNIX for Dummies Questions & Answers | 4 | 04-13-2009 08:47 AM |
| rename multiple files | a_dor8 | Shell Programming and Scripting | 3 | 04-12-2009 08:33 PM |
| rename multiple files | antointoronto | Shell Programming and Scripting | 13 | 03-20-2008 10:16 AM |
|
|