views:

114

answers:

4

I have a bunch of files that are named 'something_12345.doc' (any 5-digit number, not necessarily 12345). I need to rename them all to just 'something.doc'. This is a Unix filesystem, and I suspect there's a way to do this with just one command... Can any Unix regular expressions guru help?

Thanks!

A: 

Yes, rename takes perl style regular expressions. Do a man rename.

dirkgently
Not always. On RH systems it does a simple string replacement.
Ignacio Vazquez-Abrams
+2  A: 

rename 's/_[0-9][0-9][0-9][0-9][0-9]//' *.doc

bhups
I'm on FreeBSD, so this doesn't work.. bash: /usr/local/bin/rename: Argument list too longI can't figure out how to set variables for rename
Figured it out.for i in [A-Za-z]*_[0-9][0-9][0-9][0-9][0-9].doc; do mv "$i" "${i/_[0-9][0-9][0-9][0-9][0-9]}"; done
+1  A: 

@OP, the shell has already expanding your pattern for you, there in your mv statement, you don't have to specify the pattern for 5 digits again.

for file in *_[0-9][0-9][0-9][0-9][0-9].doc
do
  echo mv "$file" "${file%_*}.doc"
done
ghostdog74