Bash script for renaming files (all odd)
I have a ton of picture files that I would like to rename.
I'm very new to bash, could someone help me out? What I need is a script that will go down a directory and rename the files from P1180001.JPG, P1180002.JPG, ... to 0001.JPG, 0003.JPG, 0005.JPG ... I also have another directory that looks just like this one as far as file name, but I need to rename them starting with 6, and I need them even: P1180001.JPG, P1180002.JPG, ... to 0006.JPG, 0008.JPG, 0010.JPG ... How can this be done? Thanks. |
Can you provide a snippet of shell script that you are trying.....I might be able to modify it to fullfill the purpose.
Quote:
|
nevermind, I realize my solution won't work ... solution pending.
|
If I am reading your requirements correctly, you have a list of directories containing a number of duplicate filenames and you want all these files to have unique file names in another folder.
This code assumes that the files you want renamed have an extension of uppercase JPG. I am renaming the files to lowercase extension to stop them being picked up in any subsequent run. However, they would be overwritten so be careful! Code:
### I was using cp -p instead of mv for testing purposes, I would suggest that you do the same as this will not remove your original files. Once you are happy that you have all your files you can then remove the originals (or re-run after changing the cp -p statements to mv) |
Hmm, it didn't work out the way I wanted it to. Let me try explaining it a little better.
I have two directories. One named 'Odd' and one named 'Even'. I am looking for a script (or two separate ones, one for Odd and one for Even) that will go down the files in order and rename them accordingly. Both directories have files in this exact format: Code:
P1180001.JPG Code:
P1180001.JPG --> 0001.jpg Code:
P1180001.JPG --> 0002.jpg I appreciate it, Nick |
Perhaps awk?
e.g for odd, something like this.. (not tested): Code:
for oldfile in `ls` Big Vern |
Read your post wrong, but I have a idea on what may work in bash.
|
Thanks guys.
I tried out Big Vern's method and it worked so that's the one I'm using. The only thing he forgot to do is put $oldfile in place of $filename. Thanks. |
try this for first 'odd' directory:
#!/bin/bash echo "Enter the number of last file i.e. P11800013.JPG enter only 13" read f for ((i=$f;i>1;i=i-1)) do s=$((i*2-1)) mv P118000$i.JPG P118000$s.JPG done You may use cp instead of mv to be secure. If successful modify it to create 'even' directory. Quote:
|
Something simple for the odd numbers.
Code:
#!/bin/bash |
Quote:
|
All times are GMT -5. The time now is 12:54 PM. |