LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   rename script all files in dir (https://www.linuxquestions.org/questions/linux-newbie-8/rename-script-all-files-in-dir-201911/)

Longinus 07-06-2004 05:25 PM

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

synapse 07-06-2004 05:34 PM

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

Basslord1124 07-06-2004 05:39 PM

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.
}

Dark_Helmet 07-06-2004 09:24 PM

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 :) )

Longinus 07-06-2004 11:13 PM

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

Dark_Helmet 07-06-2004 11:34 PM

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.

Cyberian 08-01-2004 11:08 PM

How do I use a bash script?

Tinkster 08-01-2004 11:39 PM

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

Cyberian 08-02-2004 12:24 AM

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

Thanks for the help.


All times are GMT -5. The time now is 05:32 PM.