LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   rename several files sequentially (https://www.linuxquestions.org/questions/programming-9/rename-several-files-sequentially-874523/)

docaia 04-12-2011 03:36 AM

rename several files sequentially
 
Hello everyone,
I am trying to rename some files that do not have a pattern in their names to a sequential names. original file names are in the form of REC92837498, REC9837449 and so on.
I want to rename them to REC_1, REC_2...etc.

I used the following script:
Code:

j=1
for i in $(ls -rt REC*)
do
/path/${i} /path/REC_${j}
j = {j} + 1
done

however the value of j does not increase, when I do sh -x to trouble shoot the code, it says command not found.
what is the proper command to increment j by 1?

Thanks.

docaia 04-12-2011 03:38 AM

Thanks everyone,
I found the problem
I should use j = $((j+1)

colucix 04-12-2011 03:50 AM

Or simply
Code:

(( j++ ))
without the dollar sign and using the C notation for incrementing a variable by one. BTW your script above contains some errors (but most likely they're typos). Have you tried it? Be sure to have a backup copy of the original files or perform tests on some (dummy) files placed into another directory.

grail 04-12-2011 08:12 AM

Also, have a look here at why using ls to feed a loop is inherently dangerous.
Try something as simple as:
Code:

for i in REC*

docaia 04-13-2011 04:42 AM

Thanks Colucix and Grail for your replies.
Colucix I tried the script and it worked, may be you mean I should reframe it in a more professional way, can you please provide an example on to correct the code?

Thanks again.


All times are GMT -5. The time now is 11:48 AM.