解释起来非常简单:
如何重命名
/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
以及能够在原点返回的反函数?
这种事情非常简单,但不幸的是,谷歌完全不可能,除非你已经知道如何做到这一点......
我在考虑:
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
但不幸的是,事情并非那么简单......
答案 0 :(得分:4)
对于简单的字符串后缀和前缀操作,我建议您熟悉shell的标准parameter expansion features ${VAR%suffix_to_remove}
和${VAR#prefix_to_remove}
。这些适用于任何标准sh
,而不仅仅是bash
:
test - > TEST2
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
test2 - >测试类似:
...
BASENAME=${NAME%.test2.html} # strip off .test2.html
...
答案 1 :(得分:2)
您可以使用open
命令。
rename
或
rename 's/(.*)\./$12./' *.html
答案 2 :(得分:1)
你可以这样做:
name1="/folder1/something.test.html"
name2="${name1/%.html/2.html}"
echo "$name2"
#mv "$name1" "$name2"
输出:
/folder1/something.test2.html
.html
正在字符串末尾替换为2.html
。
返回test2 -> test
:
name2="/folder1/something.test2.html"
name1="${name2/%2.html/.html}"