简体   繁体   English

如何使用Shell脚本重命名文件?

[英]How can I rename files with a shell script?

I have files named like this: 我有这样命名的文件:

  • 2014-8-1.txt 2014-8-1.txt
  • 2014-08-02.txt 2014-08-02.txt
  • 2014-9-03.txt 2014-9-03.txt
  • 2014-09-4.txt 2014-09-4.txt

How can I rename those files to the following names? 如何将这些文件重命名为以下名称? ('-' hyphens will be replaced with '_' underscores.) (“-”连字符将替换为“ _”下划线。)

  • 2014_8_1.txt 2014_8_1.txt
  • 2014_08_02.txt 2014_08_02.txt
  • 2014_9_03.txt 2014_9_03.txt
  • 2014_09_4.txt 2014_09_4.txt

Try this: 尝试这个:

mv `ls *-*.txt` `ls *-*.txt | sed 's/-/_/g'`

mv - renames file mv-重命名文件
ls with wildcard - will list all the files with "-" in it 带通配符的ls-将列出所有带有“-”的文件
sed - will search for - in filename and replace it with _ any number of occurances. sed-将在文件名中搜索-并将其替换为_,且不限次数。

您可以使用以下find

find . -type f -name [0-9]*-[0-9]*-[0-9]*.txt -print0 | xargs -0 -I {} bash -c 'f="{}"; echo mv "$f" "${f//-/_}"'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM