LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-12-2011, 04:36 AM   #1
docaia
LQ Newbie
 
Registered: Jan 2008
Posts: 18

Rep: Reputation: 0
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.
 
Old 04-12-2011, 04:38 AM   #2
docaia
LQ Newbie
 
Registered: Jan 2008
Posts: 18

Original Poster
Rep: Reputation: 0
Thanks everyone,
I found the problem
I should use j = $((j+1)
 
Old 04-12-2011, 04:50 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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.
 
Old 04-12-2011, 09:12 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,564

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
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*
 
Old 04-13-2011, 05:42 AM   #5
docaia
LQ Newbie
 
Registered: Jan 2008
Posts: 18

Original Poster
Rep: Reputation: 0
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Application to eliminate doubles in files and rename changed files with date? S. Chapelin Linux - Software 6 01-16-2011 03:02 AM
Rename Directories Sequentially (Hexadecimal Format) kiranmannava Linux - Newbie 2 11-17-2009 06:45 PM
Trouble with making a bash script to read in different files and rename output files. rystke Linux - Software 1 05-07-2009 09:00 AM
mulitple copy a existing directory and rename it at the end sequentially in BASH neo2k Linux - Newbie 2 08-20-2008 11:45 AM
rename files linux2man Linux - General 8 02-03-2007 06:26 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration