LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-27-2009, 07:25 PM   #1
tiaratiara
LQ Newbie
 
Registered: Feb 2009
Posts: 7

Rep: Reputation: 0
mass rename files


I tried this to rename lots of html files(ex. weraw.html, wer1.html.html)into cc_1.html,cc_2.html,cc_3.html etc.


#!/bin/bash
for i in *.html
do
x="$i"
mv $i "cc_"$x.html
done


I guess the error lies in not successfully change i into a string.
How to fix that?
 
Old 02-27-2009, 07:44 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
I think you're missing something - why don't you replace the mv with an echo of $i and $x.
Might give you some hints.
 
Old 02-27-2009, 08:22 PM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Is `i' supposed to be an integer, or the original name of the file? Your description indicates an integer, while your code indicates that you want to precede each filename with cc_.

Code:
# Produce an array of the original files
original_files=(*.html)

# Use the index + 1 to rename the file:
for (( index=0; index<${#original_files[@]}; index++ )); do
  mv "${original_files[$index]}" "cc_$(($index+1)).html"
done
Code:
for file in *.html; do
  mv "${file}" cc_"${file}"
done
If these files contain links to each other, you will be creating problems for yourself by breaking these links.
 
Old 02-27-2009, 08:49 PM   #4
tiaratiara
LQ Newbie
 
Registered: Feb 2009
Posts: 7

Original Poster
Rep: Reputation: 0
mass rename files

Thanks you for your advice!!!
I want i to be an integer but I don't set well...

I just done this by

#!/bin/bash
j=1;
for i in *; do
if [[ $j -lt 1000 ]]; then
mv "${i}" "cc_${j}.html";

fi;
j=$(($j+1));

done
 
  


Reply



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
LXer: Mass-Rename Files Using an Easy CLI Method LXer Syndicated Linux News 0 07-07-2008 06:20 PM
mass mp3 rename using tr mk6032 Linux - Newbie 2 04-28-2008 06:03 PM
mass rename files cope Programming 4 10-28-2007 11:33 AM
BASH USAGE: mass files rename sirpelidor Linux - General 8 09-20-2005 12:36 AM
Mass Rename wickdgin Linux - Newbie 2 04-13-2003 02:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:10 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
Open Source Consulting | Domain Registration