Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to rename a lot of files on Mac OS X (10.7.2).. I don't have the perl package for the rename command.

My files have names like "T452-102456-0.png" and I want to delete the "-0" part. I know I can do this action by writing my own php-cli script, but I would like to know of an easier and faster solution.

share|improve this question
    
Isn't there logical contradiction between “a lot of pdf files” and “My files have names like "T452-102456-0.png"”? –  manatwork Nov 7 '11 at 12:26
    
Oh... the type of files doesn't matter, but it's true ^^. I edit, thank you for your comment =) –  Raphaël Nov 7 '11 at 13:14
add comment

2 Answers

up vote 11 down vote accepted

Bash or Ksh together with mv could solve it:

for f in *.png; do mv -n "$f" "${f/-0}"; done

In case the file name may have “0” after the first dash too and the “-0” is always in front of the dot, you may want to include that dot too in the expression:

for f in *.png; do mv -n "$f" "${f/-0./.}"; done

But as that renaming rule is simple, if you have rename from the util-linux package, that will do it too:

rename '-0.' '.' *.png
share|improve this answer
    
it works perfectly. Thank you very much =) –  Raphaël Nov 7 '11 at 13:13
add comment

With zsh:

autoload zmv
zmv '(*)-0(.png)' '$1$2'

With other shells:

for x in *-0.png; do mv -- "$x" "${x%-0.*}.png"; done

If you have files in subdirectories as well, with zsh:

zmv '(**/)(*)-0(.png)' '$1$2$3'

With ksh93:

set -o globstar
for x in **/*-0.png; do mv -- "$x" "${x%-0.*}.png"; done

With bash ≥4, as above, but use shopt -s globstar instead of the set command.

With other shells:

find -name '*-0.png' -exec sh -c 'for x; do mv -- "$x" "${x%-0.*}.png"; done' _ {} +
share|improve this answer
add comment

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.