Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

5 Answers 5

up vote 4 down vote accepted

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
share|improve this answer
    
This worked, but i'm really curious why rename isnt working. –  Mech Software Mar 24 '10 at 18:39
    
let's suppose the name of the picture has a "." in it before the .jpg :) –  Jimmy 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 Software 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

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/' *
share|improve this answer
    
I tried a variant of that, and I tried what you have listed there. Didnt work either. –  Mech Software Mar 24 '10 at 18:36

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

share|improve this answer

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
share|improve this answer

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

rename 's/(\w+).jpg/$1\_00.jpg/' *
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.