LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   batch renaming of files (https://www.linuxquestions.org/questions/programming-9/batch-renaming-of-files-576499/)

sycamorex 08-12-2007 05:19 AM

batch renaming of files
 
Hi,
I've got a number of photos that need changing their names.
What I need is a short script to change the names to 01.jpg, 02.jpg, etc.
I've been trying to do it with 'rename' and 'move' but I can't get it right.

Also, I've started learning python, and am courious how it would look in python.


thanks

FredrikN 08-12-2007 05:41 AM

Hello
Try something like this, it should work fine but I do not
have time to try it out.

Remember to try it on some dummy files first.

Quote:

#!/bin/sh
loop=1;

IFS='
'
for FILE in `ls -t -r pictures`;
do

#Get file extension
EXTENSION=`echo $FILE | sed 's/.*[.]//g'`

NEW_NAME=`$loop.$EXTENSION`

let loop++;

mv pictures/$FILE pictures/$NEW_NAME

done

sycamorex 08-12-2007 10:26 AM

Thanks

however, I get the following error:




Code:

loop=1;

#!/bin/sh
IFS='
'
for FILE in `ls -t -r zdjecia`;
do

#Get file extension
EXTENSION=`echo $FILE | sed 's/.*[.]//g'`

NEW_NAME=`$loop.$EXTENSION`

let loop++;

mv zdjecia/$FILE zdjecia/$NEW_NAME

done

Code:

mv: `zdjecia/dsc_0546.jpg' and `zdjecia/dsc_0546.jpg' are the same file
./skrypt: line 13: 65.jpg: command not found
mv: `zdjecia/dsc_0545.jpg' and `zdjecia/dsc_0545.jpg' are the same file
./skrypt: line 13: 66.jpg: command not found
mv: `zdjecia/dsc_0542.jpg' and `zdjecia/dsc_0542.jpg' are the same file
./skrypt: line 13: 67.jpg: command not found
mv: `zdjecia/dsc_0539.jpg' and `zdjecia/dsc_0539.jpg' are the same file
./skrypt: line 13: 68.jpg: command not found
mv: `zdjecia/dsc_0498a.jpg' and `zdjecia/dsc_0498a.jpg' are the same file
./skrypt: line 13: 69.jpg: command not found
mv: `zdjecia/dsc_0438.jpg' and `zdjecia/dsc_0438.jpg' are the same file
./skrypt: line 13: 70.jpg: command not found
mv: `zdjecia/dsc_0420.jpg' and `zdjecia/dsc_0420.jpg' are the same file
./skrypt: line 13: 71.jpg: command not found
mv: `zdjecia/dsc_0419.jpg' and `zdjecia/dsc_0419.jpg' are the same file
./skrypt: line 13: 72.jpg: command not found
mv: `zdjecia/dsc_0417.jpg' and `zdjecia/dsc_0417.jpg' are the same file
./skrypt: line 13: 73.jpg: command not found
mv: `zdjecia/dsc_0982.jpg' and `zdjecia/dsc_0982.jpg' are the same file

and so on...

any ideas?
thank you

FredrikN 08-12-2007 10:58 AM

Right, the backticks should not be there.
Remove the backticks and it will work.

Replace

NEW_NAME=`$loop.$EXTENSION`

with

NEW_NAME=$loop.$EXTENSION

sycamorex 08-12-2007 11:21 AM

Works perfect, thanks

muha 08-12-2007 01:30 PM

The obligatory one-liner:
Code:

j=1;for i in *;do echo mv $i $j.jpg;j=$((j+1));done
Remove the echo do let it do the actual work.

ghostdog74 08-12-2007 09:47 PM

Quote:

Originally Posted by sycamorex (Post 2856174)
Also, I've started learning python, and am courious how it would look in python.

Code:

import os
os.chdir("where")
for num,files in enumerate(glob.glob("*.jpg")):
    s=os.path.splitext(files)
    fname,ext=s[0],s[1]
    if num +1  < 10: n=str(num+1).zfill(2)   
    os.rename(files,s[0]+str(n)+s[1])


ghostdog74 08-12-2007 10:08 PM

Quote:

Originally Posted by muha (Post 2856493)
The obligatory one-liner:
Code:

j=1;for i in *;do echo mv $i $j.jpg;j=$((j+1));done
Remove the echo do let it do the actual work.

The requirement needs to include a 0 infront of single digit numbers.

FredrikN 08-13-2007 12:17 AM

Quote:

Originally Posted by muha (Post 2856493)
The obligatory one-liner:
Code:

j=1;for i in *;do echo mv $i $j.jpg;j=$((j+1));done
Remove the echo do let it do the actual work.

Works fine so long as all pictures are jpg files. If there is a mix of jpg and jpeg the code will not work.

sycamorex 08-13-2007 02:24 AM

thanks guys


All times are GMT -5. The time now is 03:53 PM.