LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Renaming files (https://www.linuxquestions.org/questions/linux-newbie-8/renaming-files-675351/)

Viablade 10-09-2008 12:52 PM

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

sycamorex 10-09-2008 01:00 PM

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.

Viablade 10-09-2008 01:54 PM

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

sycamorex 10-09-2008 02:27 PM

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


jasohl 10-09-2008 02:40 PM

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

cyprinidae 10-09-2008 03:08 PM

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..)

jasohl 10-09-2008 03:30 PM

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...

jgallo 10-09-2008 03:47 PM

ya just run
Code:

mv ./rename myscript
then
Code:

./myscript

ghostdog74 10-10-2008 08:29 AM

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.

i92guboj 10-10-2008 08:59 AM

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.

Viablade 10-10-2008 01:26 PM

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

dv502 10-10-2008 01:54 PM

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.


All times are GMT -5. The time now is 02:16 PM.