LinuxQuestions.org
Review your favorite Linux distribution.
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 10-06-2003, 01:05 PM   #1
joe_stevensen
LQ Newbie
 
Registered: Jun 2001
Location: San Jose, CA
Distribution: Debian (Servers); Gentoo (Desktop)
Posts: 29

Rep: Reputation: 15
Need script to rename files


I have a bunch of randomly name .jpg files and wish to rename them all to:

hawaii001.jpg
hawaii002.jpg
hawaii003.jpg
hawaii004.jpg
..etc..

I don't care what order the files get renamed.
I have searched all over and tried to do it myself to no avail.
All the files will be *.jpg in a single folder.
Thanks.
 
Old 10-06-2003, 01:36 PM   #2
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 50
how about something like this (not tested btw)

COUNTER=0; for i in `ls | grep 'jpg$'`; do mv $i $COUNTER.jpg; COUNTER=$(($COUNTER + 1)); done
 
Old 10-06-2003, 01:43 PM   #3
joe_stevensen
LQ Newbie
 
Registered: Jun 2001
Location: San Jose, CA
Distribution: Debian (Servers); Gentoo (Desktop)
Posts: 29

Original Poster
Rep: Reputation: 15
that worked great.
thanks.
 
Old 10-06-2003, 01:44 PM   #4
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
What about:
Code:
#!/bin/bash

# Config
dir="/home/joe/pics/hawaii/"         # Full path including trailing slash
prefix="hawaii"                      # Filename prefix
suffix=".jpg"                        # Filename suffix


# Script
num=1
for file in `ls $dir`;do
mv $dir$file $dir$prefix$num$suffix
num=$(($num+1))
done
 
Old 10-06-2003, 08:17 PM   #5
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
man rename
Code:
SYNOPSIS
       rename from to file...

DESCRIPTION
       rename  will  rename  the specified files by replacing the first occur-
       rence of from in their name by to.

       For example, given the files foo1, ..., foo9, foo10, ...,  foo278,  the
       commands

              rename foo foo0 foo?
              rename foo foo0 foo??

       will turn them into foo001, ..., foo009, foo010, ..., foo278.

       And
              rename .htm .html *.htm

       will fix the extension of your html files.

SEE ALSO
       mv(1)
 
Old 12-05-2003, 06:12 PM   #6
xscd
LQ Newbie
 
Registered: Dec 2003
Location: New Mexico, US
Distribution: FreeBSD
Posts: 2

Rep: Reputation: 0
mvb -- batch rename shell (sh or bash) script

You could also check out mvb, a shell script to quickly rename many files in the current working directory, great for digital camera users or those who download images from the Internet. Changes generic or bad filenames to better ones.

mvb at icewalkers.com:
http://www.icewalkers.com/Linux/Soft...20460/mvb.html
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Bash Script - Rename Multiple Files embsupafly Programming 16 04-02-2010 03:50 AM
Batch Script to rename files... jamie_barrow Linux - Newbie 16 06-14-2009 01:26 PM
File rename script jjwl89 Linux - General 1 03-14-2005 06:28 PM
rename script all files in dir Longinus Linux - Newbie 8 08-02-2004 12:24 AM
Got a script to rename a batch of files? jamie_barrow Linux - General 1 08-08-2003 06:52 AM


All times are GMT -5. The time now is 08:27 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 Google+: linuxquestions
Open Source Consulting | Domain Registration