LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux 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
 
LinkBack Search this Thread
Old 11-09-2009, 02:28 AM   #1
jiapei100
Member
 
Registered: Jan 2005
Location: England
Distribution: Ubuntu 9.10
Posts: 52

Rep: Reputation: 15
Exclamation Use Linux script to batch change file names in a folder


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
 
Old 11-09-2009, 02:59 AM   #2
David the H.
Senior Member
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 4,825

Rep: Reputation: 989Reputation: 989Reputation: 989Reputation: 989Reputation: 989Reputation: 989Reputation: 989Reputation: 989
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?)
 
Old 11-09-2009, 03:04 AM   #3
evo2
Senior Member
 
Registered: Jan 2009
Location: Japan
Distribution: Debian
Posts: 2,500

Rep: Reputation: 321Reputation: 321Reputation: 321Reputation: 321
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.
 
Old 11-09-2009, 06:33 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 233Reputation: 233Reputation: 233
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..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are 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 05:31 PM.

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
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration