I have a bunch of files that are named 'something_12345.doc' (any 5-digit number, n开发者_Python百科ot 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!
@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
This question was asked a lot of times on SO:
- bash script to rename all files in a directory?
- bash Linux - Massive folder rename
- How to do a mass rename?
- https://stackoverflow.com/questions/7137/replacing-one-string-in-a-bunch-of-file-names-with-another
My personal preference goes to mmv. But see "Mass Rename/copy/link Tools".
rename 's/_[0-9][0-9][0-9][0-9][0-9]//' *.doc
use sed
ls *.doc | sed 's:\([^0-9_]\)[0-9_][0-9_]*\.doc:$(mv & \1.doc)' | /bin/bash
Yes, rename
takes perl style regular expressions. Do a man rename
.
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.
精彩评论