LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-12-2007, 05:19 AM   #1
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
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
 
Old 08-12-2007, 05:41 AM   #2
FredrikN
Member
 
Registered: Nov 2001
Location: Sweden
Distribution: GNU/Linux since -97
Posts: 149

Rep: Reputation: 15
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

Last edited by FredrikN; 08-12-2007 at 11:10 AM.
 
Old 08-12-2007, 10:26 AM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836

Original Poster
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
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
 
Old 08-12-2007, 10:58 AM   #4
FredrikN
Member
 
Registered: Nov 2001
Location: Sweden
Distribution: GNU/Linux since -97
Posts: 149

Rep: Reputation: 15
Right, the backticks should not be there.
Remove the backticks and it will work.

Replace

NEW_NAME=`$loop.$EXTENSION`

with

NEW_NAME=$loop.$EXTENSION

Last edited by FredrikN; 08-12-2007 at 11:06 AM.
 
Old 08-12-2007, 11:21 AM   #5
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836

Original Poster
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Works perfect, thanks
 
Old 08-12-2007, 01:30 PM   #6
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 38
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.
 
Old 08-12-2007, 09:47 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by sycamorex View Post
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])
 
Old 08-12-2007, 10:08 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by muha View Post
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.
 
Old 08-13-2007, 12:17 AM   #9
FredrikN
Member
 
Registered: Nov 2001
Location: Sweden
Distribution: GNU/Linux since -97
Posts: 149

Rep: Reputation: 15
Quote:
Originally Posted by muha View Post
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.
 
Old 08-13-2007, 02:24 AM   #10
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836

Original Poster
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
thanks guys
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
help renaming files please balistic Linux - Newbie 2 07-29-2007 03:35 PM
Batch renaming of dig. camera pics? fbfd1338 Linux - Newbie 2 04-09-2006 05:00 AM
Batch Renaming in bash xushi Programming 6 07-07-2005 03:24 PM
renaming batch of files linux_ub Linux - Newbie 6 10-27-2004 09:41 PM
Renaming files in one go saurya_s Linux - Software 1 01-12-2004 01:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration