1

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!

user261231
  • 49
  • 1
  • 3

6 Answers6

3

@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
  • 327,991
  • 56
  • 259
  • 343
3

This question was asked a lot of times on SO:

My personal preference goes to mmv. But see "Mass Rename/copy/link Tools".

Community
  • 1
  • 1
bortzmeyer
  • 34,164
  • 12
  • 67
  • 91
2

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

bhups
  • 14,345
  • 8
  • 49
  • 57
  • I'm on FreeBSD, so this doesn't work.. bash: /usr/local/bin/rename: Argument list too long I can't figure out how to set variables for rename – user261231 Jan 28 '10 at 21:34
  • 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 – user261231 Jan 28 '10 at 22:14
1

use sed

ls *.doc | sed 's:\([^0-9_]\)[0-9_][0-9_]*\.doc:$(mv & \1.doc)' | /bin/bash
Igor Jerosimić
  • 13,621
  • 6
  • 44
  • 53
mohitgupta
  • 11
  • 1
0

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

dirkgently
  • 108,024
  • 16
  • 131
  • 187
0

On FreeBSD, you might be interested in the sysutils/renameutils port. The command qmv opens your $EDITOR and allows you to specify all file renames in a reasonably comfortable environment. I personally prefer the qmv -fdo (single column) format.

jkerian
  • 16,497
  • 3
  • 46
  • 59