1

What am I missing here, this seems so simple yet I cant get it to work.

I have a directory with files like AGPNDRAH01.jpg

I want a directory with files like AGPNDRAH01_00.jpg

rename 's/(\w+).jpg\$1\_00.jpg$//' *

Doesnt, work. Centos linux. Makes no sense to me why this isnt working.

Mech
  • 660
  • 2
  • 10
  • 22

6 Answers6

4

Quick hack that will do what you describe in bash:

cd /directory
for F in `ls -1 |awk -F. '{print $2}'`
do
  mv $F.jpg ${F}_00.jpg
done

For rename usage:

wmoore@bitbucket(/tmp/dowork)$ ls -1
1.jpg
2.jpg
3.jpg
4.jpg
5.jpg
wmoore@bitbucket(/tmp/dowork)$ rename .jpg _00.jpg *.jpg
wmoore@bitbucket(/tmp/dowork)$ ls -1
1_00.jpg
2_00.jpg
3_00.jpg
4_00.jpg
5_00.jpg
Warner
  • 23,440
  • 2
  • 57
  • 69
  • This worked, but i'm really curious why rename isnt working. – Mech Mar 24 '10 at 18:39
  • let's suppose the name of the picture has a "." in it before the .jpg :) –  Mar 24 '10 at 18:42
  • That did it you man page must be better equipped than mine. rename .jpg _00.jpg *.jpg did it. Thanks! – Mech Mar 24 '10 at 18:44
  • Then I'd suppose you'd be using a non-standard naming convention that my systems wouldn't employ, Marcel. ;) – Warner Mar 24 '10 at 18:48
2

Try the following bash script:

find . -type f -name "*.jpg" -print | while read FILE  
  do mv "${FILE}" "`dirname ${FILE}`/`basename ${FILE} .jpg`_00.jpg"
  done

That will find all .jpg files in or below the current directory and insert _00 before .jpg. If you only want it to handle the current directory start the find command with find . -maxdepth 1

Cry Havok
  • 1,825
  • 13
  • 10
1

Double backslash at the end of the regex means substitute s/(\w+).jpg$1_00.jpg$ with an empty string.

I used this:

rename 's/\.jpg/_00.jpg/' *.jpg
0

I am not familiar with rename, but your regex doesn't look right. It appears like you are trying to match files named like this AGPNDRAH01.jpg.AGPNDRAH01_00.jpg and change their name to nothing.

Are you sure you don't mean something like this instead?

rename 's/(\w+).jpg$/\$1\_00.jpg/' *
Zoredache
  • 128,755
  • 40
  • 271
  • 413
  • I tried a variant of that, and I tried what you have listed there. Didnt work either. – Mech Mar 24 '10 at 18:36
0

I have a variant of redhat, and rename doesn't support the 's/' command. Here's a one-liner for you (You have to backslash parenthesis and plus-signs to get the functionality in sed):

for fl in *.jpg; do mv $fl `echo $fl | sed 's/\(\w\+\).jpg/\1_00.jpg/'`; done
bradlis7
  • 353
  • 1
  • 5
  • 16
0

You just had a slash in the wrong place and an unnecessary backslash.

rename 's/(\w+).jpg/$1\_00.jpg/' *
Dennis Williamson
  • 60,515
  • 14
  • 113
  • 148