LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-02-2006, 06:46 PM   #1
Freestone
Member
 
Registered: Jan 2003
Location: Brighton, Michigan
Distribution: FC5
Posts: 114

Rep: Reputation: 15
rename large qty. of files via script


Hello,

I have been beating my head against the wall trying to figure this out. Its probably very simple, so here goes.

I have a directory full of audio files and I used grip to get them off of the cd. The problem I have is
most of the file names are wrong and I want to rename them with a one line bash script (or command).
Example:
Acts-Chpt-01.mp3
Acts-Chpt-02.mp3
Acts-Chpt-03.mp3
....
....
Acts-Chpt-15.mp3
Acts-Chpt-16.mp3

When the files were written, the chapter numbers that were written are wrong. Chapter number 1
should actually be 12, 2 should be 13, 3 should be 14, etc. BTW, files 1-9 starts with '0'....
I have a ton of files that I need to rename and I am looking for a shortcut. I created a new directory to practice in and created a bunch of files with the 'touch' command. (I'm not going to practice on the real thing). touch file-01.mp3 file-02.mp3 etc. I read the man and info pages on 'tr' but I am getting nowhere. Can someone offer a suggestion for batch renaming files with numbers in the filename and increasing or decreasing the actual number in the filename without changing anything else?

Thanks loads!!
 
Old 10-02-2006, 07:07 PM   #2
metalx1000
Member
 
Registered: Jun 2006
Distribution: Debian
Posts: 112

Rep: Reputation: 16
This should do the trick.
Code:
#!/bin/bash
echo -n "Enter name: "
read input
echo -n "Enter starting number: "
read count


for name in *mp3
do
	mv $name $input$count.mp3
	count=$(($count + 1))
done

echo "Files Renamed"
 
Old 10-02-2006, 07:24 PM   #3
Freestone
Member
 
Registered: Jan 2003
Location: Brighton, Michigan
Distribution: FC5
Posts: 114

Original Poster
Rep: Reputation: 15
metalx1000,

I'll mess around with that script. It definitely needs a little tweaking but it got me started in the right direction. I ran it and I'm sharing the output with you...
I named the script 'rename'.

Quote:
08:13 PM /home/ab : ls
Acts-Chpt-01.mp3 Acts-Chpt-05.mp3 Acts-Chpt-09.mp3 Acts-Chpt-13.mp3
Acts-Chpt-02.mp3 Acts-Chpt-06.mp3 Acts-Chpt-10.mp3 Acts-Chpt-14.mp3
Acts-Chpt-03.mp3 Acts-Chpt-07.mp3 Acts-Chpt-11.mp3 Acts-Chpt-15.mp3
Acts-Chpt-04.mp3 Acts-Chpt-08.mp3 Acts-Chpt-12.mp3
08:13 PM /home/ab : rename

Enter name: Acts-Chpt-01.mp3
Enter starting number: 12
Files Renamed

08:14 PM /home/ab : ls
Acts-Chpt-01.mp312.mp3 Acts-Chpt-01.mp317.mp3 Acts-Chpt-01.mp322.mp3
Acts-Chpt-01.mp313.mp3 Acts-Chpt-01.mp318.mp3 Acts-Chpt-01.mp323.mp3
Acts-Chpt-01.mp314.mp3 Acts-Chpt-01.mp319.mp3 Acts-Chpt-01.mp324.mp3
Acts-Chpt-01.mp315.mp3 Acts-Chpt-01.mp320.mp3 Acts-Chpt-01.mp325.mp3
Acts-Chpt-01.mp316.mp3 Acts-Chpt-01.mp321.mp3 Acts-Chpt-01.mp326.mp3
08:14 PM /home/ab :
Part of the filenames obviously need to be removed. I'll work on the script and if you think of a fix then please post it. I appreciate your input!! Thanks for your help!
 
Old 10-02-2006, 07:32 PM   #4
metalx1000
Member
 
Registered: Jun 2006
Distribution: Debian
Posts: 112

Rep: Reputation: 16
My bad, I just realize I did put any comments in my script.

at the part where it asks for the 'name' you should put the first part of the name

Example.
Code:
Enter name: Acts-Chpt-
Enter starting number: 12
NOT
Code:
Enter name: Acts-Chpt-01.mp3
Enter starting number: 12
sorry about that.
 
Old 10-02-2006, 08:11 PM   #5
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD, Raspbian, Arch
Posts: 2,331

Rep: Reputation: 357Reputation: 357Reputation: 357Reputation: 357
If you're willing to consider an alternate suggestion...

There's a really nice (free) program I used to use for this back when I used Windows. MP3Tag. It's a file renamer, tag editor, etc. Really nice for organizing a music collection.

Looks like it runs on Linux under Wine. I haven't tried it personally, but check out the following post for info: http://www.anytag.de/forums/index.php?showtopic=3646
 
Old 10-03-2006, 06:24 PM   #6
Freestone
Member
 
Registered: Jan 2003
Location: Brighton, Michigan
Distribution: FC5
Posts: 114

Original Poster
Rep: Reputation: 15
Its heading in the right direction. I just need to add something to the script that recognizes if I use the number '1' as the starting number....

Let me try it this way: For the numbers '1-9', I need the script to recognize that and add a '0' before the digits '1-9'. I was looking through 'rute' ( the tutorial) and found a simple script in there and he uses
something along the lines of:

while test "$N" -le "10"
For some reason the -le command isn't being recognized by my bash.

Am I making any sense?
What I'm looking for is a line I can add to the script that utilizes 'if $input <= 10 then add a zero before the digits '1-9' so I get 01, 02, 03, 04...and when it gets to 10 just print 10, 11, 12 and so on?
Any ideas?

This post is a bit scattered as I pressed for time. Sorry for any confusion.

Thanks.
 
Old 10-03-2006, 06:35 PM   #7
fudam
Member
 
Registered: Feb 2006
Distribution: FC
Posts: 86

Rep: Reputation: 15
Freestone,

I'm a bit confused, if you're adding 11 to each number ("1 should actually be 12, 2 should be 13, 3 should be 14, etc.") then how does "01" come into play? There should be no 01 after you add 11 to all your numbers.

Maybe you can give two concrete examples (one single digit number and one double) of what the files ARE now and what they SHOULD be after.

-fudam
 
Old 10-03-2006, 06:55 PM   #8
Freestone
Member
 
Registered: Jan 2003
Location: Brighton, Michigan
Distribution: FC5
Posts: 114

Original Poster
Rep: Reputation: 15
fudam,

Sorry for the confusion.

If I have a bunch of files and the beginning number is '1' and the last one is '15' and I want them to be renamed 12.mp3, 13.mp3, 14.mp3, etc, then thats one scenario.
Some of the files are named 16.mp3, 17.mp3, 18.mp3, etc and I may want them to be 06,.mp3, 07.mp3, 08.mp3, etc. The reason I'm looking for a '0' before the digits '1-9' is so the .mp3 files play in the proper order... if you have files named 1.mp3, 14.mp3, 10.mp3, 11.mp3, 13.mp3 etc the file and 'ls', then the following is how it is listed:

07:53 PM /home/agg : ls
10.mp3 11.mp3 13.mp3 14.mp3 1.mp3
10 is listed before 1.
I need that '0'!!!!

Does that clear things up?
 
Old 10-03-2006, 07:28 PM   #9
fudam
Member
 
Registered: Feb 2006
Distribution: FC
Posts: 86

Rep: Reputation: 15
I think I understand now. These may not be the most elegant solutions but they should work. This is based on your naming structure given above (Acts-Chpt-01.mp3).

To add 11:
Code:
for i in *; 
do base=${i%-*}; extension=${i##*.}; 
num=$(echo ${i%.*} |cut -d '-' -f3 | cut -d '.' -f1); 
newnum=$num+11; mv "$i"  "$base-$newnum.$extension"; 
done;
To add the 0:
Code:
for i in *; 
do base=${i%-*}; extension=${i##*.}; 
num=$(echo ${i%.*} |cut -d '-' -f3 | cut -d '.' -f1); 
let newnum=$(printf %02d $num); 
mv "$i"  "$base-$newnum.$extension"; 
done;
Like I said, not very elegant but it works. If you want something more universal I would tread into the realm of perl myself.

Last edited by fudam; 10-03-2006 at 07:30 PM.
 
Old 10-04-2006, 06:30 AM   #10
Freestone
Member
 
Registered: Jan 2003
Location: Brighton, Michigan
Distribution: FC5
Posts: 114

Original Poster
Rep: Reputation: 15
Just to let you know, that doesn't work.

I'm just looking for a line to add to the original script that will add a '0' before the digits '1-9'.

What would the syntax be in bash for a number being less than 10?
I know its not complicated...

if $i < '10'
then blah blah
else blah blah
fi

Is the '<' the correct symbol for 'less than' or is there some other way in bash to represent this?

Anyone?

Thanks!
 
Old 10-04-2006, 10:04 AM   #11
metalx1000
Member
 
Registered: Jun 2006
Distribution: Debian
Posts: 112

Rep: Reputation: 16
try this
Code:
#!/bin/bash
echo -n "Enter name: "
read input
echo -n "Enter starting number: "
read count

for name in *mp3 
do

if [ $count -lt 10 ];
then
	count="0$count"
echo "hello"
fi
	
	mv $name $input$count.mp3
	count=$(($count + 1))

done

echo "Files Renamed"
 
Old 10-04-2006, 10:31 AM   #12
Freestone
Member
 
Registered: Jan 2003
Location: Brighton, Michigan
Distribution: FC5
Posts: 114

Original Poster
Rep: Reputation: 15
metalx1000,

The script is making sense to me and I'm understanding it well except for one thing.
It does work up to a point now ( the script you just put on here this morning)
Here is the directory listing before and after the script and the error message included:

Quote:
11:25 AM /home/ab : c;ls
Acts-Chpt-11.wav Acts-Chpt-14.wav Acts-Chpt-17.wav Acts-Chpt-20.wav
Acts-Chpt-12.wav Acts-Chpt-15.wav Acts-Chpt-18.wav Acts-Chpt-21.wav
Acts-Chpt-13.wav Acts-Chpt-16.wav Acts-Chpt-19.wav Acts-Chpt-22.wav
11:25 AM /home/ab : lt9
Enter name: Acts-Chapter-
Enter starting number: 1
hello
hello
hello
hello
hello
hello
hello
hello
/bin/lt9: line 17: 08: value too great for base (error token is "08")
Files Renamed
11:25 AM /home/ab : ls
Acts-Chapter-01.mp3 Acts-Chapter-04.mp3 Acts-Chapter-07.mp3 Acts-Chpt-20.wav
Acts-Chapter-02.mp3 Acts-Chapter-05.mp3 Acts-Chapter-08.mp3 Acts-Chpt-21.wav
Acts-Chapter-03.mp3 Acts-Chapter-06.mp3 Acts-Chpt-19.wav Acts-Chpt-22.wav
((Don't pay attention to the change in the extension as that is minor, because I'm going to implement 'lame' into the script to convert the files from .wav>.mp3.))
The error from line 17 is throwing me for a loop. Other than that, its doing exactly what I was looking for. Thank you so much!! Any ideas for a fix on this? I don't understand 'value to great for base'.
I'm assuming that bash is treating "-lt 10" as digits '0-8' as 0 thru 8 is actually nine digits.
Thanks for your patience and help!!
 
Old 10-04-2006, 11:21 AM   #13
metalx1000
Member
 
Registered: Jun 2006
Distribution: Debian
Posts: 112

Rep: Reputation: 16
There might be a better way, but this should work.

Code:
#!/bin/bash
echo -n "Enter name: "
read input
echo -n "Enter starting number: "
read count


for name in *mp3
do

if [ $count -gt 10 -o $count -eq 10 ]
then
	count=$count
	mv $name $input$count.mp3
	count=$(($count + 1))
fi

if [ $count -eq "9" ]
then
	count="0$count"
	mv $name $input$count.mp3
	count=10
fi

if [ $count -eq "8" ]
then
	count="0$count"
	mv $name $input$count.mp3
	count=9
fi


if [ $count -lt "8" ]
then
	count="0$count"
	mv $name $input$count.mp3
	count=$(($count + 1))
fi
	


done

echo "Files Renamed"
 
Old 10-04-2006, 11:46 AM   #14
Freestone
Member
 
Registered: Jan 2003
Location: Brighton, Michigan
Distribution: FC5
Posts: 114

Original Poster
Rep: Reputation: 15
Success!!

Thank you!

I need to learn something here so I do have a question. In line 11:
Quote:
if [ $count -gt 10 -o $count -eq 10 ]
I understand the '-gt' and the '-eq'. But between the two, what is the funcion of '-o'?

Again, thanks and after this this thread is dead.
 
Old 10-04-2006, 03:32 PM   #15
metalx1000
Member
 
Registered: Jun 2006
Distribution: Debian
Posts: 112

Rep: Reputation: 16
the '-o' is the 'or' command. So the 'if' statement is true if it is greater then or equeal to '10'
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Batch Script to rename files... jamie_barrow Linux - Newbie 16 06-14-2009 01:26 PM
Script to Rename Many Files geeman2.0 Programming 3 04-05-2006 01:45 PM
rename script all files in dir Longinus Linux - Newbie 8 08-02-2004 12:24 AM
Need script to rename files joe_stevensen Programming 5 12-05-2003 06:12 PM
Got a script to rename a batch of files? jamie_barrow Linux - General 1 08-08-2003 06:52 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:35 AM.

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