LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 07-06-2004, 05:25 PM   #1
Longinus
Member
 
Registered: Sep 2003
Distribution: Redhat 9.0 && Slackware 9.1
Posts: 420

Rep: Reputation: 30
rename script all files in dir


how would i make a bash script to rename all files in a directory in a consecutive manner like...

1.jpg
2.jpg
3.jpg
4.jpg
etc...

any suggestions?
tanks
 
Old 07-06-2004, 05:34 PM   #2
synapse
Member
 
Registered: Jan 2004
Location: On Planet Earth.
Distribution: Slackware 12
Posts: 244

Rep: Reputation: 30
Hi ok
I did the following , still learning so bear with it !

#!/bin/bash

COUNT=1
for input in `cat dir.txt`; do
FILENAME=$input
if test "$COUNT" -ge "100"; then
mv $FILENAME $COUNT.png
COUNT=$[COUNT+1]

elif test "$COUNT" -ge "10"; then
mv $FILENAME 0$COUNT.png
COUNT=$[COUNT+1]

elif test "$COUNT" -le "9"; then
mv $FILENAME 00$COUNT.png
COUNT=$[COUNT+1]
fi
shift
done

Its a complicated procedure but it worked for me, I have seen much simpler ways of doing this, but ey im learning.

before you run this script just do a ls >dir.txt in the directory that has the jpegs in it and then edit the dir.txt file and remove the . and .. directorys that will be listed there, also place the script in the working directory.
cheers
 
Old 07-06-2004, 05:39 PM   #3
Basslord1124
Member
 
Registered: Jun 2004
Location: KY
Distribution: Debian, Mint, Puppy
Posts: 507

Rep: Reputation: 51
Looks like nested if's there. I have never messed with scripts in Linux (I have messed with C++ though) but you can do what you are looking in a for loop. Something like this:

for (variable i=0; variable < what number you wish to stop atl; variable +1)
{
whatever action you want performed.
}
 
Old 07-06-2004, 09:24 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Here's a slightly more condensed way of doing it in bash. I thought I would throw it in in case synapse might like to look at it, and that it might be a little easier to follow
Code:
#!/bin/bash

extension=".jpg"

old_ifs=${IFS}
IFS=$'\n'

current_number=1
for filename in *${extension} ; do
  mv -v ${filename} ${current_number}${extension}
  ((current_number=current_number+1))
done

IFS=${old_ifs}
This will only modify jpg files. If you need something else, then just change the extension variable appropriately.

The IFS business is to prevent the script from choking on filenames with spaces.

Although, synapse's script will put leading 0's in front of the filename to keep them organized. This one won't do that, and may lead to some confusion in listing the files. Reasonably easily fixed later with a sed command or two if it becomes a nuisance (or you could use synapse's script )

Last edited by Dark_Helmet; 07-06-2004 at 09:28 PM.
 
Old 07-06-2004, 11:13 PM   #5
Longinus
Member
 
Registered: Sep 2003
Distribution: Redhat 9.0 && Slackware 9.1
Posts: 420

Original Poster
Rep: Reputation: 30
cool thanks guys

yeah im trying to learn bash myself

hey Dark_Helmet why do you have to do:

((current_number=current_number+1))

in a nested parentheses?

why wont bash let you just do:

current_number=current_number+1

?
thanks
 
Old 07-06-2004, 11:34 PM   #6
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
I'm not absolutely positive, but I'm sure it's primarily to remove ambiguity.

In shell scripts, you'll notice the "natural" format for data is a string/text. It makes sense because commands are text and a vast number of files are text (configuration, data, etc.). So, if you were to say:
Code:
document_number=document_number+1
document_number=${document_number}+1
For the first, are you trying to assign the string "document_number+1" to a variable or are you trying to add 1 to a variable? If you're trying to add 1, then it'd probably make sense to reference the contents of the variable, right? So that brings us to the second statement. However, since a bash script has no specific data types, it can't know if the contents of document_number is a string or an actual number. Besides, having a string with "something+1" might be a legitimate argument to pass for a program. It would also be a potential error for it to convert the contents to an integer as a convenience. There may be some legitimate use for a string of the form "59test+1". So, converting "59test" to 59 to perform the addition could potentially derail the purpose of the script completely.

So, the solution the bash maintainers picked was to force the user to explicitly declare when you want to do math. You can do it with the nested parentheses like I did, or with the "let" keyword, like so:
Code:
((document_number=document_number+1))
let document_number=document_number+1
They're both equivalent. I use the parentheses for a couple reasons. It's easy two just double-punch '(' than type "let " (ultra laziness I admit), emacs does parenthesis matching making sure my expression is as expected, and I think it sets off the code a little more making it obvious this is a numeric calculation rather than a textual operation.

Last edited by Dark_Helmet; 07-06-2004 at 11:36 PM.
 
Old 08-01-2004, 11:08 PM   #7
Cyberian
Member
 
Registered: May 2004
Distribution: SuSE
Posts: 117

Rep: Reputation: 15
How do I use a bash script?
 
Old 08-01-2004, 11:39 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Basically by typing its name, followed by ENTER
But you could have started a thread for that
question, rather than digging up a long-gone
one ... ;)


Cheers,
Tink
 
Old 08-02-2004, 12:24 AM   #9
Cyberian
Member
 
Registered: May 2004
Distribution: SuSE
Posts: 117

Rep: Reputation: 15
Heh, I did not look at the date. I just simply googled and got this thread.

Thanks for the help.
 
  


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
Help with Bash Script - Rename Multiple Files embsupafly Programming 16 04-02-2010 03:50 AM
Batch Script to rename files... jamie_barrow Linux - Newbie 16 06-14-2009 01:26 PM
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
bash script to rm all files in a dir keirobyn Programming 8 07-19-2002 07:53 AM

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

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