bash 如何使用bash脚本重命名目录中的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18348957/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:19:13  来源:igfitidea点击:

How to rename files in a directory using bash script

linuxbash

提问by Abhishek Gupta

I wanted to find a bash script for renaming files with _ (underscore) to - (hyphen)

我想找到一个 bash 脚本,用于将带有 _(下划线)的文件重命名为 -(连字符)

for example changing file name my_page_name.php to my-page-name.php, keeping the name of the file of those without .php extension same

例如将文件名 my_page_name.php 更改为 my-page-name.php,保持没有 .php 扩展名的文件名相同

I attempted: Nothing yet , just used the script found here bbs.archlinux.org/viewtopic.php?id=36305 and replace space with _ and underscore with -

我尝试过:什么都没有,只是使用了在这里找到的脚本 bbs.archlinux.org/viewtopic.php?id=36305 并用 _ 替换空格并用 - 替换下划线

回答by rici

If you only need to do it in one directory (and not subdirectories):

如果您只需要在一个目录(而不是子目录)中执行此操作:

for f in *_*; do mv "$f" "${f//_/-}"; done

Otherwise, you can use findto -execa bash subshell.

否则,你可以使用find-exec一个bash子shell。

回答by Fabio A. Correa

Use the renameprogram:

使用rename程序:

rename s:_:-: *.php