LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 11-09-2009, 01:28 AM   #1
jiapei100
Member
 
Registered: Jan 2005
Location: Surrey, BC, Canada
Distribution: Ubuntu 12.04
Posts: 70

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, 01:59 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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, 02:04 AM   #3
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
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, 05:33 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

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



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 On
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 12:45 PM
Batch script remove old files based on their names and keep on for each version Reda01 Programming 6 05-08-2008 02:35 PM
Script to change file names with incremental counter. rickh Linux - Software 2 05-01-2006 10:43 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:34 PM.

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