LinuxQuestions.org
Visit Jeremy's Blog.
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 09-13-2008, 04:49 AM   #1
bkcreddy17
Member
 
Registered: Feb 2008
Location: India-Hyderabad
Distribution: RHEL and Fedora
Posts: 171

Rep: Reputation: 15
Question how to rename files


I have to upload some images in to a folder in a server. All the image files have different names. Say abc.jpeg,xyz.jpeg and ... Now i want to rename all the file as Image001, Image002,...... Changing each and every file is very difficult. And the files are in hundreds. Is there any command or script to do this?
 
Old 09-13-2008, 05:42 AM   #2
papseddy
LQ Newbie
 
Registered: Sep 2008
Location: New Delhi
Posts: 15

Rep: Reputation: 0
hi\
try this may be its work
# mv * image00.jpeg
ok
enjoy
 
Old 09-13-2008, 06:19 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by papseddy View Post
hi\
try this may be its work
# mv * image00.jpeg
ok
enjoy
No! That will rename *all* files to the same name. This will in fact delete all-except-one files.
 
Old 09-13-2008, 08:12 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
This is a beautiful problem because it covers so many things you need to know about bash.

Notice that you are using zero padded numbers. This is what makes the problem so devious! You need to have an integer variable inside a loop to keep count on which number to add, but bash goobles up leading zeros in a variable. To make things even more fun, the number of leading zeros varies from none to three.

Let's create a function to do just this part.
Code:
ascii_num ()
{
    picnum='000'$1;
    picnum=${picnum:$((-4)):4}
}
Call it like "picnum 10" to produce a variable with the value of "0010".

Now we are ready for the program:
Code:
> ls
11554.jpeg  14145.jpeg  15533.jpeg  17242.jpeg  20854.jpeg  21366.jpeg  22942.jpeg  2515.jpeg   27809.jpeg  30845.jpeg  3837.jpeg  6197.jpeg  8632.jpeg
1159.jpeg   14340.jpeg  15742.jpeg  17722.jpeg  20881.jpeg  21866.jpeg  23037.jpeg  25293.jpeg  29139.jpeg  31336.jpeg  4900.jpeg  62.jpeg    test.sh
13536.jpeg  14385.jpeg  16307.jpeg  18214.jpeg  208.jpeg    22734.jpeg  24374.jpeg  25547.jpeg  29264.jpeg  31690.jpeg  6118.jpeg  7824.jpeg  test.tar
14142.jpeg  14879.jpeg  16814.jpeg  19786.jpeg  21262.jpeg  22845.jpeg  2453.jpeg   27406.jpeg  30704.jpeg  32360.jpeg  611.jpeg   8051.jpeg
jschiwal@hpmedia:~/test/testdir> cat test.sh
ascii_num ()
{
    picnum='000'$1;
    picnum=${picnum:$((-4)):4}
}

# set the index to 0 initially
declare -i num=0

# rename each jpeg file to Image####.jpeg form
for file in *.jpeg; do
  num+=1
# convert num to a string
  ascii_num $num
#rename the file
  mv -v "$file" Image"${picnum}.jpeg"
done

jschiwal@hpmedia:~/test/testdir> ./test.sh
`11554.jpeg' -> `Image0001.jpeg'
`1159.jpeg' -> `Image0002.jpeg'
`13536.jpeg' -> `Image0003.jpeg'
`14142.jpeg' -> `Image0004.jpeg'
`14145.jpeg' -> `Image0005.jpeg'
`14340.jpeg' -> `Image0006.jpeg'
`14385.jpeg' -> `Image0007.jpeg'
`14879.jpeg' -> `Image0008.jpeg'
`15533.jpeg' -> `Image0009.jpeg'
`15742.jpeg' -> `Image0010.jpeg'
`16307.jpeg' -> `Image0011.jpeg'
`16814.jpeg' -> `Image0012.jpeg'
`17242.jpeg' -> `Image0013.jpeg'
`17722.jpeg' -> `Image0014.jpeg'
`18214.jpeg' -> `Image0015.jpeg'
`19786.jpeg' -> `Image0016.jpeg'
`20854.jpeg' -> `Image0017.jpeg'
`20881.jpeg' -> `Image0018.jpeg'
`208.jpeg' -> `Image0019.jpeg'
`21262.jpeg' -> `Image0020.jpeg'
`21366.jpeg' -> `Image0021.jpeg'
`21866.jpeg' -> `Image0022.jpeg'
`22734.jpeg' -> `Image0023.jpeg'
`22845.jpeg' -> `Image0024.jpeg'
`22942.jpeg' -> `Image0025.jpeg'
`23037.jpeg' -> `Image0026.jpeg'
`24374.jpeg' -> `Image0027.jpeg'
`2453.jpeg' -> `Image0028.jpeg'
`2515.jpeg' -> `Image0029.jpeg'
`25293.jpeg' -> `Image0030.jpeg'
`25547.jpeg' -> `Image0031.jpeg'
`27406.jpeg' -> `Image0032.jpeg'
`27809.jpeg' -> `Image0033.jpeg'
`29139.jpeg' -> `Image0034.jpeg'
`29264.jpeg' -> `Image0035.jpeg'
`30704.jpeg' -> `Image0036.jpeg'
`30845.jpeg' -> `Image0037.jpeg'
`31336.jpeg' -> `Image0038.jpeg'
`31690.jpeg' -> `Image0039.jpeg'
`32360.jpeg' -> `Image0040.jpeg'
`3837.jpeg' -> `Image0041.jpeg'
`4900.jpeg' -> `Image0042.jpeg'
`6118.jpeg' -> `Image0043.jpeg'
`611.jpeg' -> `Image0044.jpeg'
`6197.jpeg' -> `Image0045.jpeg'
`62.jpeg' -> `Image0046.jpeg'
`7824.jpeg' -> `Image0047.jpeg'
`8051.jpeg' -> `Image0048.jpeg'
`8632.jpeg' -> `Image0049.jpeg'
There are a few things to point out here. The first part of the function adds the '000' string before the number. The next line used ${variable:offset:length} with a negative offset.
There's a gotha however. ${picnum:-4:4} doesn't work right because '-4' here is a string. That is why I used $((-4)) to make certain it is an arithmetic expression.

So your simple little task involved:
  1. variable expansion
  2. functions
  3. loops
  4. $((...)) for arithmetic expressions
  5. Using "declare -i" so that the variable stays an integer.

Last edited by jschiwal; 09-13-2008 at 08:16 AM.
 
Old 09-13-2008, 07:53 PM   #5
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
I expect that it is to meet the requirements of a few free light weight slide shows viewers.

In your case, picasaweb will do it for you. Once you install it and select the folder and create a slide show, in html format, it copies the selected pictures into a sub folder (with files renamed as 001, 002 etc.) below the main html folder.

The good thing about is that it has got a version for linux (by seamlessly installing wine).

HTH.

End
 
Old 09-13-2008, 09:02 PM   #6
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Wow, jschiwal went to town!

Re: ${picnum:-4:4}. It's simpler to use a space, as in echo ${picnum: -4:4} to avoid confusion with the :- operator.

One of the assignments I used to give (ksh) shell scripting students was to create a rename script. It was incrementally built up over a couple of weeks, and resulted in a nice tool to rename files using a variety of transformations. The final result w/my answers are in week 10 Homework (and Answers) at: http://cis68b1.mikecappella.com/ . I've thought about converting all the lesson plans to bash. Perhaps some enterprising student here will undertake the challenge.

Last edited by Mr. C.; 09-13-2008 at 09:07 PM.
 
Old 09-13-2008, 10:39 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by bkcreddy17 View Post
I have to upload some images in to a folder in a server. All the image files have different names. Say abc.jpeg,xyz.jpeg and ... Now i want to rename all the file as Image001, Image002,...... Changing each and every file is very difficult. And the files are in hundreds. Is there any command or script to do this?
For a ready script, you can use the script here.
Usage:
Code:
# ./script.sh -D /path/images -s 001 -e image999 -p ".*" -d "*.jpeg" 
# ./script.sh -D /path/images -s 001 -e image999 -p ".*" "*.jpeg"  #remove -d to do actual rename
Of course, to learn about bash+tools, you should write your own using tips from Mr.C and jshiwal

Last edited by ghostdog74; 09-13-2008 at 10:40 PM.
 
Old 09-14-2008, 09:22 AM   #8
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

a simple bash example:
Code:
num=0
for i in *.jpg; do
  test -f "$i" || continue # if there are no .jpg files
  mv -i "$i" Image`printf "%3.3d" \`expr $num + 1\``.jpg
done
The -i option is to prevent from overwriting existing files.

Another nice program to to the job is mmv. See the manual for details.

Jan
 
  


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
mass rename files cope Programming 4 10-28-2007 11:33 AM
rename files linux2man Linux - General 8 02-03-2007 05:26 AM
rename files allelopath Linux - General 5 07-05-2005 03:00 AM
rename all files in my directory BabaKali Linux - Newbie 2 11-10-2004 04:27 PM
Can not rename files. Maximus2000 Linux - General 0 04-22-2004 01:36 PM

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

All times are GMT -5. The time now is 12:49 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