bash 重命名文件名的一部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1392768/
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
Renaming part of a filename
提问by not_a_geek
I have loads of files which look like this:
我有很多看起来像这样的文件:
DET01-ABC-5_50-001.dat
...
DET01-ABC-5_50-0025.dat
and I want them to look like this:
我希望它们看起来像这样:
DET01-XYZ-5_50-001.dat
...
DET01-XYZ-5_50-0025.dat
How can I do this?
我怎样才能做到这一点?
回答by Paul Dixon
There are a couple of variants of a rename command, in your case, it may be as simple as
重命名命令有几种变体,在您的情况下,它可能很简单
rename ABC XYZ *.dat
You may have a version which takes a Perl regex;
您可能有一个采用 Perl 正则表达式的版本;
rename 's/ABC/XYZ/' *.dat
回答by RJ Alten
for file in *.dat ; do mv $file ${file//ABC/XYZ} ; done
No rename
or sed
needed. Just bash parameter expansion.
不需要rename
或sed
不需要。只是 bash参数扩展。
回答by paxdiablo
Something like this will do it. The for
loop may need to be modified depending on which filenames you wish to capture.
像这样的事情会做到这一点。该for
循环可能需要根据其档案名称,你想捕捉进行修改。
for fspec1 in DET01-ABC-5_50-*.dat ; do
fspec2=$(echo ${fspec1} | sed 's/-ABC-/-XYZ-/')
mv ${fspec1} ${fspec2}
done
You should always test these scripts on copiesof your data, by the way, and in totally different directories.
顺便说一下,您应该始终在数据副本上以及在完全不同的目录中测试这些脚本。
回答by paxdiablo
You'll need to learn how to use sed http://unixhelp.ed.ac.uk/CGI/man-cgi?sed
你需要学习如何使用 sed http://unixhelp.ed.ac.uk/CGI/man-cgi?sed
And also to use for so you can loop through your file entries http://www.cyberciti.biz/faq/bash-for-loop/
并且还用于,以便您可以循环浏览您的文件条目http://www.cyberciti.biz/faq/bash-for-loop/
Your command will look something like this, I don't have a term beside me so I can't check
你的命令看起来像这样,我身边没有一个术语,所以我无法检查
for i in `dir` do mv $i `echo $i | sed '/orig/new/g'`
回答by stribika
I like to do this with sed. In you case:
我喜欢用 sed 来做这件事。在你的情况下:
for x in DET01-*.dat; do
echo $x | sed -r 's/DET01-ABC-(.+)\.dat/mv -v "##代码##" "DET01-XYZ-.dat"/'
done | sh -e
It is best to omit the "sh -e" part first to see what will be executed.
最好先省略“sh -e”部分,看看会执行什么。
回答by Sam Bisbee
All of these answers are simple and good. However, I always like to add an interactive mode to these scripts so that I can find false positives.
所有这些答案都很简单而且很好。但是,我总是喜欢为这些脚本添加交互模式,以便我可以发现误报。
if [[ -n $inInteractiveMode ]]
then
echo -e -n "$oldFileName => $newFileName\nDo you want to do this change? [Y/n]: "
read run[[ -z $run || "$run" == "y" || "$run" == "Y" ]] && mv "$oldFileName" "$newFileName"
fi
if [[ -n $inInteractiveMode ]]
then
echo -e -n "$oldFileName => $newFileName\nDo you want to do this change? [Y/n]: "
read run[[ -z $run || "$run" == "y" || "$run" == "Y" ]] && mv "$oldFileName" "$newFileName"
fi
Or make interactive mode the default and add a force flag (-f | --force) for automated scripts or if you're feeling daring. And this doesn't slow you down too much: the default response is "yes, I do want to rename" so you can just hit the enter key at each prompt (because of the ``-z $run\
test.
或者将交互模式设为默认模式,并为自动化脚本添加强制标志 (-f | --force),或者如果您感到大胆。这不会让你太慢:默认响应是“是的,我确实想重命名”,所以你可以在每个提示下按回车键(因为 ``-z $run\
测试。