如何使用 BASH 从文件名中删除特定字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1500204/
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
How do I Remove Specific Characters From File Names Using BASH
提问by resolveaswontfix
I have a lot of files that have a shared pattern in their name that I would like to remove. For example I have the files, "a_file000.tga" and "another_file000.tga". I would like to do an operation on those files that would remove the pattern "000" from their names resulting in the new names, "a_file.tga" and "another_file.tga".
我有很多文件的名称中有共享模式,我想删除它们。例如,我有文件“a_file000.tga”和“another_file000.tga”。我想对这些文件进行操作,从它们的名称中删除模式“000”,从而产生新名称“a_file.tga”和“another_file.tga”。
回答by Paused until further notice.
Bash can do sed
-like substitutions:
Bash 可以做sed
类似替换:
for file in *; do mv "${file}" "${file/000/}"; done
回答by mouviciel
Try this (this works in plain old Bourne sh
as well):
试试这个(这也适用于普通的老伯恩sh
):
for i in *000.tga
do
mv "$i" "`echo $i | sed 's/000//'`"
done
Both arguments are wrapped in quotes to support spaces in the filenames.
两个参数都用引号括起来以支持文件名中的空格。
回答by Cascabel
A non-bash solution, since I know two speedy posters have already covered that:
一个非 bash 解决方案,因为我知道两个快速的海报已经涵盖了:
There's an excellent short perl program called rename
which is installed by default on some systems (others have a less useful rename program). It lets you use perl regex for your renaming, e.g:
有一个优秀的简短 perl 程序rename
,它默认安装在某些系统上(其他系统的重命名程序不太有用)。它允许您使用 perl regex 进行重命名,例如:
rename 's/000//' *000*.tga
回答by DigitalRoss
#!/bin/bash
ls | while read name; do
echo mv $name ${name///}
done
回答by Dhruv Pal
Use rename, maybe you need to install it on linux, Its python script
使用rename,也许你需要在linux上安装它,它的python脚本
rename (option) 's/oldname/newname' ...
so you can use it like
所以你可以像这样使用它
rename -v 's/000//' *.tga
that means we are instructing to replace all files with .tga extension in that folder to replace 000
with empty space. Hope that works
这意味着我们正在指示将该文件夹中所有扩展名为 .tga 的文件替换000
为空白空间。希望有效