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 |
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 |
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. } |
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 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 :) ) |
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 |
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 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)) |
How do I use a bash script?
|
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 |
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 04:50 PM. |