LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-23-2005, 09:36 PM   #1
jeffreybluml
Member
 
Registered: Mar 2004
Location: Minnesota
Distribution: Fedora Core 1, Mandrake 10
Posts: 405

Rep: Reputation: 30
script to change multiple filenames in a directory


I imagine this would be simple, hoping somebody can help me out.

I just need a script that, when run in a directory, will take all of the image files in there and rename numerically. For instance, let's say I've got a directory with 10 .jpg images in it, all named randomly, and mostly with words as opposed to numbers (depends on whether the wife or I took the pics off the camera). All have the same file extension though, I imagine that'll help.

So, when run, I'd just like it to rename the images (in which order I guess I really don't care) starting with 1.jpg, 2.jpg, 3.jpg, etc...

Any help?

Thanks in advance...
 
Old 03-23-2005, 09:50 PM   #2
mjrich
Senior Member
 
Registered: Dec 2001
Location: New Zealand
Distribution: Debian
Posts: 1,046

Rep: Reputation: 45
There are hundreds of threads covering this on LQ and elsewhere...

Best to do a search, and have a quick read of http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html and http://www.tldp.org/LDP/abs/html. For starters though, something like this will do the trick...
Code:
for f in *.jpg; do
     echo Renaming $f to $g.jpg...
     mv $f $g.jpg
done
I'll leave it up to you to figure out the the last bit (incrementing g).

Cheers,

mj
 
Old 03-23-2005, 09:55 PM   #3
xowl
Member
 
Registered: Jan 2005
Distribution: Slackware current (and others)
Posts: 188

Rep: Reputation: 30
U could try this:


cd MyDirectory
j=1; for i in $(ls --color=none); do mv $i $j.jpg; j=$(($j+1)); done


Tell me if it worked

Last edited by xowl; 03-23-2005 at 09:59 PM.
 
Old 03-23-2005, 09:56 PM   #4
visaris
Member
 
Registered: Dec 2004
Distribution: gentoo
Posts: 190

Rep: Reputation: 30
Code:
prompt # let "j = 1"; for file in * ; do cp $file $j.jpg; let "j += 1"; done
That worked for me in bash. Give it a try (on a copy to be safe).
 
Old 03-24-2005, 05:50 AM   #5
jeffreybluml
Member
 
Registered: Mar 2004
Location: Minnesota
Distribution: Fedora Core 1, Mandrake 10
Posts: 405

Original Poster
Rep: Reputation: 30
Thanks folks! They all work wonderfully...
 
Old 11-19-2006, 03:25 PM   #6
peter88
LQ Newbie
 
Registered: Nov 2006
Posts: 20

Rep: Reputation: 0
script to change multiple filenames in a directory

Thx for above script, works I want to achieve a slight variation on the theme, any pointers appreciated:

where its standard to be able to set an intger counter to count asin "j += 1", what I wish to achieve is to be able to increment ascii or unicode values. So for example lets say you had digital pic DSC001.jpg, DSC002.jpg .... DSCnnn.jpg, Now lets say I wanted to create a script and be able to pass it an argument as the $1 parameter such that the file(s) would be renamed to A.jpg, B.jpg...Z.jpg, OR aa.jpg, bb.jpg...zz.jpg, or aa.jpg, ab.jpb...az.jpg., how would I achieve this? Is there a function in bash or other Linux shells which enables me to do this?

A second related (or perhaps not ) question is as follows: When you have a file in a directory which begins with a hyphen ( - ), the mv cmd fails. Normally putting quotes around the filename does the trick, however lets say you have 5 files in a dir and one is called --abc def ghi.jpg , then of course you need to use "" around it to handle the spaces, but the wildcard does not work properly.

So :
$ cd /tmp
$ mv *.* /usr/bin/ will not work properly (* or *.*) and the --abc def ghi.jpg file will not be moved. How can I do this so that files which begin with a - and also contain spaces are moved?

TIA
 
Old 11-27-2006, 03:23 PM   #7
xowl
Member
 
Registered: Jan 2005
Distribution: Slackware current (and others)
Posts: 188

Rep: Reputation: 30
Hi.

Actually, I don't know a "decent" method to do what you want to do just by using bash. It's possible to do it by using another scripting language joined with bash.

The only "pure bash" approach that I found out was something like

Code:
chars="a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9"
files=$(ls --color=none )

for r in $files; do
  echo $r
done | {
for i in $chars; do
  for j in $chars; do
    for k in $chars; do
      NewFileName=$i$j$k.jpg
      OldFileName=$(line)
      [ "$OldFileName" != "" ] &&  echo -n moving $OldFileName into $NewFileName
      [ "$OldFileName" != "" ] &&  mv $OldFileName $NewFileName
      [ "$OldFileName" != "" ] &&  echo ". Done"
    done
  done
done
}
Remember to set an appropiate comodin in the $files variable declaration.

Tell me if this works.


Camilo
 
Old 12-03-2006, 01:13 AM   #8
xowl
Member
 
Registered: Jan 2005
Distribution: Slackware current (and others)
Posts: 188

Rep: Reputation: 30
The solution to the second problem is pretty easy:

to move the file "-camilo" to the file "asd" use:

Code:
mv ./-camilo asd
and voila!!

good louck

Camilo
 
Old 12-06-2006, 01:46 AM   #9
peter88
LQ Newbie
 
Registered: Nov 2006
Posts: 20

Rep: Reputation: 0
xowl, thanks very much for your response, I will give it a go
 
  


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
How to change directory in a shell script? jdupre Linux - Newbie 13 06-25-2015 05:03 AM
script to remove spaces from multiple filenames jeffreybluml Linux - Newbie 36 07-31-2013 02:10 AM
Script to change names of files in a directory geomonap Linux - General 2 12-03-2004 03:04 PM
script to change directory trout21 Linux - Newbie 9 02-17-2004 12:49 PM
Changing multiple filenames with a script brecki Linux - Newbie 8 01-30-2004 03:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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