改变——到。对于目录中的所有文件

发布于 2024-09-02 15:54:56 字数 312 浏览 2 评论 0原文

我需要重命名目录中的所有文件。源文件名的一些示例如下:

alpha--sometext.381928
comp--moretext.7294058

结果文件将重命名为:

alpha.sometext.381928
comp.moretext.7294058

-- 之前和之后的字符数不一致

该脚本需要在当前安装的 Ubuntu 和 FreeBSD 上运行。这些是精简的 LAMP 服务器,因此仅安装了必要的软件包。

谢谢

I need to rename all the files in a directory. Some examples of the source filenames are:

alpha--sometext.381928
comp--moretext.7294058

The resultant files would be renamed as:

alpha.sometext.381928
comp.moretext.7294058

The number of characters before and after the -- is not consistant.

The script needs to work on current installations of Ubuntu and FreeBSD. These are lean LAMP servers so only the necessary packages have been installed.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟─花易冷 2024-09-09 15:54:57

作为纯 shell 的替代方案,您可以使用 rename,这是 Perl 附带的标准实用程序。对于简单的情况来说更方便。

rename 's/--/./' *

As an alternative to pure shell, you can use rename, a standard utility that comes with Perl. It's more convenient for simple cases.

rename 's/--/./' *
千秋岁 2024-09-09 15:54:57

通过 bash:

for file in *--*; do
    mv "${file}" "${file/--/.}"
done

神奇之处在于 ${file/--/.} ,它是 ${file} 的值,除了每个“--”更改为“ ”。

Via bash:

for file in *--*; do
    mv "${file}" "${file/--/.}"
done

The magic is in ${file/--/.} which is the value of ${file} except with each "--" changed to a "."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文