重命名文件名的另一个名字 [英] Rename filename to another name

查看:145
本文介绍了重命名文件名的另一个名字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非常简单的解释:

如何重命名

/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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆