bash 在“重命名”中使用通配符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12601373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Wildcards with 'rename'
提问by heds1
I have been using the renamecommand to batch rename files. Up to now, I have had files like:
我一直在使用该rename命令批量重命名文件。到目前为止,我有这样的文件:
2010.306.18.08.11.0000.BO.ADM..BHZ.SAC
2010.306.18.08.11.0000.BO.AMM..BHZ.SAC
2010.306.18.08.11.0000.BO.ASI..BHE.SAC
2010.306.18.08.11.0000.BO.ASI..BHZ.SAC
and using rename 2010.306.18.08.11.0000.BO. "" *and rename .. _. *I have reduced them to:
并使用rename 2010.306.18.08.11.0000.BO. "" *和rename .. _. *我将它们减少到:
ADM_.BHZ.SAC
AMM_.BHZ.SAC
ASI_.BHE.SAC
ASI_.BHZ.SAC
which is exactly what I want. A bit clumsy, I guess, but it works. The problem occurs now that I have files like:
这正是我想要的。我想有点笨拙,但它有效。现在问题出现了,我有这样的文件:
2010.306.18.06.12.8195.TW.MASB..BHE.SAC
2010.306.18.06.14.7695.TW.CHGB..BHN.SAC
2010.306.18.06.24.4195.TW.NNSB..BHZ.SAC
2010.306.18.06.25.0695.TW.SSLB..BHZ.SAC
which exist in the same folder. I have been trying to get the similar results to above using wildcards in the renamecommand eg. rename 2010.306.18.*.*.*.*. ""but this appends the first appearance of 2010.306.18.*.*.*.*.to the beginning of all the other files - clearly not what I'm after, such that I get:
存在于同一文件夹中。我一直试图在rename命令中使用通配符获得与上述类似的结果,例如。rename 2010.306.18.*.*.*.*. ""但这将 的第一次出现附加2010.306.18.*.*.*.*.到所有其他文件的开头 - 显然不是我所追求的,这样我得到:
2010.306.18.06.12.8195.TW.MASB..BHE.SAC
2010.306.18.06.12.8195.TW.MASB..BHE.SAC2010.306.18.06.14.7695.TW.CHGB..BHN.SAC
2010.306.18.06.12.8195.TW.MASB..BHE.SAC2010.306.18.06.24.4195.TW.NNSB..BHZ.SAC
2010.306.18.06.12.8195.TW.MASB..BHE.SAC2010.306.18.06.25.0695.TW.SSLB..BHZ.SAC
I guess I am not understanding a fairly fundamental principal of wildcards here so, can someone please explain why this doesn't work and what I can do to get the desired result (preferably using rename).
我想我不理解通配符的一个相当基本的原理,所以有人可以解释为什么这不起作用以及我可以做些什么来获得所需的结果(最好使用rename)。
N.B.
NB
To clarify, the output wants to be:
为了澄清,输出希望是:
ADM_.BHZ.SAC
AMM_.BHZ.SAC
ASI_.BHE.SAC
ASI_.BHZ.SAC
MASB.BHE.SAC
CHGB.BHN.SAC
NNSB.BHZ.SAC
SSLB.BHZ.SAC
回答by xoid
You can try this first to see what commands would be executed
你可以先试试这个,看看会执行什么命令
for f in *; do echo mv $f `echo $f | sed 's/2010.*.TW.//'` ; done
If it's what you expect, you can remove echofrom the command to execute
如果这是您所期望的,您可以echo从命令中删除以执行
for f in *; do mv $f `echo $f | sed 's/2010.*.TW.//'` ; done
回答by dogbane
renamedoes not allow wildcards in the fromand tostrings. When you run rename 2010.306.18.*.*.*.*. "" *it is actually your shell which first expands the wildcard and then passes the result of the expansion to rename, hence why it does not work.
rename不允许在from和to字符串中使用通配符。当您运行rename 2010.306.18.*.*.*.*. "" *它时,实际上是您的 shell 首先扩展通配符,然后将扩展结果传递给rename,因此它不起作用。
Instead of using rename, use a loop as follows:
不要使用rename,而是使用如下循环:
for file in *
do
tmp="${file##2010*TW.}" # remove the file prefix
mv "$file" "${tmp/../_}" # replace dots with underscore
done

