LinuxQuestions.org
Help answer threads with 0 replies.
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 10-09-2008, 01:52 PM   #1
Viablade
LQ Newbie
 
Registered: Sep 2008
Posts: 9

Rep: Reputation: 0
Renaming files


I have several image files that are named

147.jpg
148.jpg
149.jpg

I need to rename all the files to look like the following

img147.jpg
img148.jpg
img149.jpg

I am having trouble writing this simple script.

Any help would be greatly appreciated.

Pierre
 
Old 10-09-2008, 02:00 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,596
Blog Entries: 1

Rep: Reputation: 1046Reputation: 1046Reputation: 1046Reputation: 1046Reputation: 1046Reputation: 1046Reputation: 1046Reputation: 1046
try:

Quote:
#!/bin/bash
for i in *.jpg
do
mv $i 'img'$i
done
It works for me, however, before you do it, back up your files.
 
Old 10-09-2008, 02:54 PM   #3
Viablade
LQ Newbie
 
Registered: Sep 2008
Posts: 9

Original Poster
Rep: Reputation: 0
i tried the script. I made it executable and this is what I see: call: rename from to files...
the files were not renamed. where did i go wrong? I made the file executable by using

chmod u+x rename

I named the file rename
 
Old 10-09-2008, 03:27 PM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,596
Blog Entries: 1

Rep: Reputation: 1046Reputation: 1046Reputation: 1046Reputation: 1046Reputation: 1046Reputation: 1046Reputation: 1046Reputation: 1046
Make sure you issue the command in the directory where the files are. The script itself needs to be either in the same directory or make sure it's included in your $PATH variable.

Code:
[xtd8865@localhost lopa]$ ls
147.jpg  148.jpg  149.jpg  myscript
[xtd8865@localhost lopa]$ ./myscript
[xtd8865@localhost lopa]$ ls
img147.jpg  img148.jpg  img149.jpg  myscript
[xtd8865@localhost lopa]$
Code:
[xtd8865@localhost lopa]$ cat myscript
#!/bin/bash
for i in *.jpg
do
mv $i 'img'$i
done
 
Old 10-09-2008, 03:40 PM   #5
jasohl
Member
 
Registered: Oct 2006
Location: Seattle, Washington
Distribution: Gentoo, LinuxMint, Arch Linux
Posts: 99

Rep: Reputation: 18
if you have the rename command on your system just use that.
the syntax is

rename from to files

Code:
rename 1 img1 ./*.jpg
or something like that.
you can use it to do all sorts of interesting things
if you want to include spaces use quotes
Code:
rename "a song" "a great song" *.mp3
a song1.mp3 > a great song1.mp3
a song i like.mp3 > a great song i like.mp3
You like a song.mp3 > You like a great song.mp3
etc.

i have found it useful anyways
 
Old 10-09-2008, 04:08 PM   #6
cyprinidae
Member
 
Registered: Oct 2008
Distribution: Fedora, CentOS, Crunchbang
Posts: 46

Rep: Reputation: 16
Viablade,
It seems that by naming your script 'rename' you tried to use
/usr/bin/rename command. You need to either run your script with full path (or just ./rename if you are in this dir) or just RENAME your 'rename' script (I mean, change it's name..)
 
Old 10-09-2008, 04:30 PM   #7
jasohl
Member
 
Registered: Oct 2006
Location: Seattle, Washington
Distribution: Gentoo, LinuxMint, Arch Linux
Posts: 99

Rep: Reputation: 18
cyprinidae's right
when you run rename your actually running the command that i mentioned.
Quote:
I made it executable and this is what I see: call: rename from to files...
 
Old 10-09-2008, 04:47 PM   #8
jgallo
Member
 
Registered: Sep 2008
Posts: 39

Rep: Reputation: 15
ya just run
Code:
mv ./rename myscript
then
Code:
./myscript

Last edited by Tinkster; 10-30-2010 at 04:56 PM.
 
Old 10-10-2008, 09:29 AM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
if you have Python, you can use the script in my sig called File Renamer
eg usage
Code:
# filerenamer.py -i "img" -l "*.jpg"
==>>>>  [ /home/147.jpg ]==>[ /home/img147.jpg ]
==>>>>  [ /home/149.jpg ]==>[ /home/img149.jpg ]
==>>>>  [ /home/148.jpg ]==>[ /home/img148.jpg ]
remove -l to commit.
 
Old 10-10-2008, 09:59 AM   #10
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,049

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
I really wouldn't bother writing such a simple script, it's less time consuming (and less prone to problems like the one that arose) to just do "for i in *.jpg; do echo mv "$i" "img$i"; done", if you like the results, remove the "echo" to apply the changes.
 
Old 10-10-2008, 02:26 PM   #11
Viablade
LQ Newbie
 
Registered: Sep 2008
Posts: 9

Original Poster
Rep: Reputation: 0
i finally got it to work. I also needed to change the extension from .JPG to .jpg. I tried this
#!/bin/bash

for filename in *
do
echo "mv -v ${filename} ${filename}.jpg"
# mv -v ${filename} ${filename}.jpg
done
This changed my files to filename.JPG.jpg instead of filename.jpg

Last edited by Viablade; 10-10-2008 at 02:27 PM. Reason: forgot something
 
Old 10-10-2008, 02:54 PM   #12
dv502
Member
 
Registered: Sep 2006
Location: USA - NYC
Distribution: Whatever icon you see!
Posts: 642

Rep: Reputation: 57
Code:
#!/bin/bash

prefix=img
for i in *.[Jj]*
do
  ext=`echo "$i" | sed -e 's/.*\.//'`
  mv "$i" "$prefix${i%$ext}$ext"
done
This script will do the job of renaming your jpeg files with the prefix img at the beginning of each file and keeping the file extension the same.

Last edited by dv502; 10-10-2008 at 09:14 PM.
 
  


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
Renaming files leupi Linux - General 5 09-16-2008 04:22 AM
help renaming files please balistic Linux - Newbie 2 07-29-2007 04:35 PM
help renaming files balistic Linux - Newbie 4 07-29-2007 09:04 AM
renaming files starwarsfan982 Linux - Software 7 10-30-2006 03:06 PM
renaming files TomalakBORG Linux - Newbie 4 12-24-2005 11:14 AM


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