重命名文件名的另一个名字 [英] Rename filename to another name
问题描述
非常简单的解释:
如何重命名
/folder1/something.test.html
/folder1/somethingElse.test.html
/folder2/againsomething.test.html
/canBeHereAlso.test.html
到
/folder1/something.test2.html
/folder1/somethingElse.test2.html
/folder2/againsomething.test2.html
/canBeHereAlso.test2.html
和还逆功能,以便能够回来原点
and also the inverse-function to be able to come back at the original point ?
这样的东西是很简单,但不幸的是,完全不可能谷歌它们,除非你已经知道怎么做了......
This kind of things are really simple but unfortunately, completely impossible to Google them, unless you know already how to do it...
我在想:
for NAME in */*.test.html; do mv $NAME $NAME2.html ; done # for A -> B
for NAME in */*.test2.html ; do mv $NAME #remove last caracter.html for B --> A
但不幸的是,它不是那么简单......
But unfortunately, it's not that simple...
推荐答案
有关简单的字符串后缀,preFIX操纵,我建议你熟悉shell的标准的参数扩展功能 $ {VAR%suffix_to_remove}
和 $ {VAR#prefix_to_remove}
。这些将在任何标准的工作 SH
,而不仅仅是庆典
:
For simple string suffix and prefix manipulation, I suggest you familiarize yourself with the shell's standard parameter expansion features ${VAR%suffix_to_remove}
and ${VAR#prefix_to_remove}
. These will work on any standard sh
, not just bash
:
的测试 - >测试2 的
for NAME in */*.test.html; do # NAME is, e.g., "dir/foo.test.html"
BASENAME=${NAME%.test.html} # BASENAME is "dir/foo"
mv "$NAME" "$BASENAME.test2.html";
done
的测试2 - >测试的是相似的:
...
BASENAME=${NAME%.test2.html} # strip off .test2.html
...
您也可以使用标准shell实用工具 基本名
和 目录名
来实现类似的东西。
You could also use the standard shell utilities basename
and dirname
to achieve something similar.
这篇关于重命名文件名的另一个名字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!