bash 如何在bash脚本中使用for循环复制和重命名文件?

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

how to copy and rename file with for loop in bash script?

linuxbashshellloops

提问by user2467583

How I can copy all file *.html to *.php with For loop?

如何使用 For 循环将所有文件 *.html 复制到 *.php?

help me...

帮我...

this my script :

这是我的脚本:

#!/bin/bash
list="$(ls *.html)"
for i in "$list"
do
  newname=$(ls "$i" | sed -e 's/html/php/')
  cat beginfile > "$newname"
  cat "$i" | sed -e '1,26d' | tac | sed -e '1,21d' | tac >> "$newname"
  cat endfiel >> "$newname"
done

or you have another ide ?

或者你有另一个想法?

回答by Chris Dodd

for f in *.html; do cp $f ${f%.html}.php; done

回答by janos

Here you go:

干得好:

for i in *.html; do mv "$i" "${i%.html}.php"; done

In RedHat Linux and derivatives there is a renameutility that simplifies this to:

在 RedHat Linux 和衍生产品中,有一个rename实用程序可以将其简化为:

rename .html .php *.html

In Debian Linux and derivatives there is a renameutility that simplifies this to:

在 Debian Linux 和衍生产品中,有一个rename实用程序可以将其简化为:

rename 's/.html$/.php/' *.html

Check man renameor rename --helpto see how to use the implementation you have. Sometimes the utility is called rename.plinstead of simply rename.

检查man renamerename --help以了解如何使用您拥有的实现。有时,实用程序被调用rename.pl而不是简单地调用rename

回答by Reinstate Monica Please

Can omit the for loop altogeather if you have perl packages installed.

如果你安装了 perl 包,可以省略 for 循环。

#!/bin/bash
rename 's/html/php/' *.html