LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash script for renaming files (all odd) (https://www.linuxquestions.org/questions/programming-9/bash-script-for-renaming-files-all-odd-617011/)

snaggletooth1134 01-28-2008 09:53 PM

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.

livetoday 01-29-2008 01:52 AM

Can you provide a snippet of shell script that you are trying.....I might be able to modify it to fullfill the purpose.



Quote:

Originally Posted by snaggletooth1134 (Post 3038340)
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.


H_TeXMeX_H 01-29-2008 02:09 AM

nevermind, I realize my solution won't work ... solution pending.

Disillusionist 01-29-2008 02:29 AM

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:

###
### Bash script to rename JPG files
###

## initialise counter
counter=1

## find all JPG files to be renamed
find . -type f -name '*.JPG'|while read l_orig
do
  if [ $counter -lt 10 ]
  then
      cp -p $l_orig img000${counter}.jpg
  else
      if [ $counter -lt 100 ]
      then
        cp -p $l_orig img00${counter}.jpg
      else
        if [ $counter -lt 1000 ]
        then
            cp -p $l_orig img0${counter}.jpg
        else
            cp -p $l_orig img${counter}.jpg
        fi
      fi
  fi

  ## Increment counter
  counter=`expr $counter + 1`
done

EDIT:-

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)

snaggletooth1134 01-29-2008 11:01 AM

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
P1180002.JPG
P1180003.JPG
P1180004.JPG
P1180005.JPG

This is what I want the script to do for the files in the 'Odd' folder:
Code:

P1180001.JPG    -->    0001.jpg
P1180002.JPG    -->    0003.jpg
P1180003.JPG    -->    0005.jpg
P1180004.JPG    -->    0007.jpg
P1180005.JPG    -->    0009.jpg

And this is what I want the script to do for the files in the 'Even' folder:
Code:

P1180001.JPG    -->    0002.jpg
P1180002.JPG    -->    0004.jpg
P1180003.JPG    -->    0006.jpg
P1180004.JPG    -->    0008.jpg
P1180005.JPG    -->    0010.jpg

Sorry for the poor explanation in my previous post.

I appreciate it,

Nick

Big_Vern 01-29-2008 01:29 PM

Perhaps awk?

e.g for odd, something like this.. (not tested):

Code:

for oldfile in `ls`
do
 newfile=`echo $oldfile | awk '{y=substr($0,5,4);z=substr($0,9); printf("%0.4d%s\n", (y*2)-1, z)}'`
 mv $filename $newfile
done

Regards,
Big Vern

davimint 01-29-2008 09:55 PM

Read your post wrong, but I have a idea on what may work in bash.

snaggletooth1134 01-29-2008 10:27 PM

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.

livetoday 01-29-2008 10:29 PM

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:

Originally Posted by snaggletooth1134 (Post 3038911)
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
P1180002.JPG
P1180003.JPG
P1180004.JPG
P1180005.JPG

This is what I want the script to do for the files in the 'Odd' folder:
Code:

P1180001.JPG    -->    0001.jpg
P1180002.JPG    -->    0003.jpg
P1180003.JPG    -->    0005.jpg
P1180004.JPG    -->    0007.jpg
P1180005.JPG    -->    0009.jpg

And this is what I want the script to do for the files in the 'Even' folder:
Code:

P1180001.JPG    -->    0002.jpg
P1180002.JPG    -->    0004.jpg
P1180003.JPG    -->    0006.jpg
P1180004.JPG    -->    0008.jpg
P1180005.JPG    -->    0010.jpg

Sorry for the poor explanation in my previous post.

I appreciate it,

Nick


davimint 01-29-2008 10:43 PM

Something simple for the odd numbers.

Code:

#!/bin/bash
#
num=1
for file in *.JPG; do
        mv "$file" "$(printf "%04u" $num).jpg"
        let num=num+2
done

Change line 3 to "num=0" for even numbers.

oldmankit 05-01-2012 12:26 AM

Quote:

Originally Posted by davimint (Post 3039552)
Something simple for the odd numbers.

Code:

#!/bin/bash
#
num=1
for file in *.JPG; do
        mv "$file" "$(printf "%04u" $num).jpg"
        let num=num+2
done

Change line 3 to "num=0" for even numbers.

I had the same needs and so was happy to find this thread. davimint - your solution worked perfectly. Thank you.


All times are GMT -5. The time now is 12:54 PM.