LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux > Linux - General
User Name
Password
Linux - General This forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Thread Tools
Old 11-09-2009, 02:28 AM   #1
jiapei100
Member
 
Registered: Jan 2005
Location: England
Distribution: Ubuntu 9.10
Posts: 49
Thanked: 0
Exclamation Use Linux script to batch change file names in a folder


[Log in to get rid of this advertisement]
I would like to change file names in two ways

1)
1.jpg -> 0001.jpg
2.jpg -> 0002.jpg
...
x.jpg -> 000x.jpg
...
xy.jpg -> 00xy.jpg

2)
5201.jpg -> 5001.jpg
5202.jpg -> 5002.jpg
...
5xyz.jpg -> 5(x-2)yz.jpg (where x >= 2)


Can anybody please be kind enough to help???

Best Regards
JIA
linuxubuntu jiapei100 is offline     Reply With Quote
Old 11-09-2009, 02:59 AM   #2
David the H.
Senior Member
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian Sid/kde3.5
Posts: 2,462
Thanked: 129
Try doing an archive or google search perhaps?

(Sorry for the snarky answer, but do you have any idea how often this kind of question comes up?)
linuxdebian David the H. is offline     Reply With Quote
Old 11-09-2009, 03:04 AM   #3
evo2
Member
 
Registered: Jan 2009
Location: Japan
Distribution: Debian
Posts: 216
Thanked: 27
Quote:
Originally Posted by jiapei100 View Post
I would like to change file names in two ways

1)
1.jpg -> 0001.jpg
2.jpg -> 0002.jpg
...
x.jpg -> 000x.jpg
...
xy.jpg -> 00xy.jpg
There are cleaner ways (which others may suggest), but for a quick and dirty approach you could use something like
Code:
for f in *.jpg ;do
  b="`basename $f .jpg`"
  mv $f `printf '%04i.jpg' $b` 
done
Quote:
Originally Posted by jiapei100 View Post
2)
5201.jpg -> 5001.jpg
5202.jpg -> 5002.jpg
...
5xyz.jpg -> 5(x-2)yz.jpg (where x >= 2)
This one is even dirtier!
Code:
for f in *.jpg ; do
  b=`basename $f .jpg`
  mv $f "`expr $b - 200`.jpg"
done
Please note that these solutions are untested and could crash and burn depending on what else is in the directory you run them in. Use at your own risk. Good luck!

Evo2.
linuxdebian evo2 is online now     Reply With Quote
Old 11-09-2009, 06:33 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 1,802
Blog Entries: 5
Thanked: 114
if you have Python 2.4+ and don't mind a ready made script, you can use this
usage:
Code:
$ ls -1
10.jpg
1.jpg
2.jpg
3.jpg
4.jpg
5.jpg
6.jpg
7.jpg
8.jpg
9.jpg

$ filerenamer.py -s ".*" -e "0001:9999.jpg" -n  -l "*.jpg"
==>>>>  [ /home/1.jpg ]==>[ /home/0001.jpg ]
==>>>>  [ /home/2.jpg ]==>[ /home/0002.jpg ]
==>>>>  [ /home/3.jpg ]==>[ /home/0003.jpg ]
==>>>>  [ /home/4.jpg ]==>[ /home/0004.jpg ]
==>>>>  [ /home/5.jpg ]==>[ /home/0005.jpg ]
==>>>>  [ /home/6.jpg ]==>[ /home/0006.jpg ]
==>>>>  [ /home/7.jpg ]==>[ /home/0007.jpg ]
==>>>>  [ /home/8.jpg ]==>[ /home/0008.jpg ]
==>>>>  [ /home/9.jpg ]==>[ /home/0009.jpg ]
==>>>>  [ /home/10.jpg ]==>[ /home/0010.jpg ]

$ ls -1
5201.jpg
5202.jpg
5203.jpg
5204.jpg
5205.jpg
5206.jpg
5207.jpg
5208.jpg
5209.jpg
5210.jpg

$ filerenamer.py -s ".*" -e "5001:5999.jpg" -n  -l "52*.jpg"
==>>>>  [ /home/5201.jpg ]==>[ /home/5001.jpg ]
==>>>>  [ /home/5202.jpg ]==>[ /home/5002.jpg ]
==>>>>  [ /home/5203.jpg ]==>[ /home/5003.jpg ]
==>>>>  [ /home/5204.jpg ]==>[ /home/5004.jpg ]
==>>>>  [ /home/5205.jpg ]==>[ /home/5005.jpg ]
==>>>>  [ /home/5206.jpg ]==>[ /home/5006.jpg ]
==>>>>  [ /home/5207.jpg ]==>[ /home/5007.jpg ]
==>>>>  [ /home/5208.jpg ]==>[ /home/5008.jpg ]
==>>>>  [ /home/5209.jpg ]==>[ /home/5009.jpg ]
==>>>>  [ /home/5210.jpg ]==>[ /home/5010.jpg ]
remove the -l option to commit changes..
linuxfedora ghostdog74 is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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
Executing a batch file on Windows from Linux using a Shell-Script paragkalra Linux - Networking 3 08-13-2008 01:45 PM
Batch script remove old files based on their names and keep on for each version Reda01 Programming 6 05-08-2008 03:35 PM
Script to change file names with incremental counter. rickh Linux - Software 2 05-01-2006 11:43 PM


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

Main Menu
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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration