bash 如何通过从文件名中删除某些字符来重命名Linux中的文件?

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

How to rename a file in linux by removing certain characters from the file name?

linuxbashshell

提问by SSS

How can I rename a file in linux to strip out certain characters from the file name?

如何在linux中重命名文件以从文件名中去除某些字符?

For example,

例如,

My123File.txt to be renamed to My123.txt

My123File.txt 重命名为 My123.txt

回答by jjlin

If you're okay with just wildcards (not full regexes), then you might try something like

如果您只接受通配符(不是完整的正则表达式),那么您可以尝试类似的方法

f='My123File.txt'
mv $f ${f/File/}

This type of shell expansion is documented here.

此处记录这种类型的外壳扩展。

If you really need regexes, try

如果您真的需要正则表达式,请尝试

f='My123File.txt'
mv $f $(echo $f | sed -e 's/File//')

回答by icyrock.com

User rename, here's a test:

用户重命名,这里是一个测试:

$ touch My123File.txt
$ rename 's/File//' My123File.txt

See man rename. renamesupports regexps, so you can do for example this - execute somewhere safe, e.g. /tmp or so:

man renamerename支持正则表达式,因此您可以执行以下操作 - 在安全的地方执行,例如 /tmp 左右:

cd /tmp
rm *.txt
touch My123File.txt My456File.txt
ls *.txt
rename 's/([A-Za-z]+)(\d+)(\w+)/-999--/' *.txt
ls *.txt

yields this:

产生这个:

My123File.txt  My456File.txt
File-999-123-My.txt  File-999-456-My.txt

回答by j?rgensen

With mmv, it's a single command a lot simpler. It also has support for translations such as lowercasing a positional parameter.

使用 mmv,它是一个简单得多的命令。它还支持翻译,例如小写位置参数。

mmv '*File.txt' '#1.txt'