bash 循环遍历不同文件并执行命令的脚本

bash script to loop through different files and perform command

我在一个目录中有 25 个文件,都命名为 xmolout1、xmolout2、...、xmolout25。

这些都是 .txt 个文件,我需要 copy the last 80 lines 从这些文件到新的 .txt 文件。

最好,这些会自动生成正确的数字(取自原始文件,例如 xmolout10 会生成 final10 等)。

之后可以删除原文件。

我是 bash 脚本的新手,我知道我可以使用 tail -80 filename.txt > newfilename.txt 复制最后 80 行,但我不知道如何实现循环。

提前致谢

如果您知道要处理的文件数量,则可以在循环中使用计数器变量:

for ((i=1; i<=25; i++))
do
    tail -80 "xmolout$i" >> "final$i"
done

如果您想与 bash 以外的 shell 保持兼容,您可以使用以下语法:

for i in {1..25}
do
    tail -80 "xmolout$i" >> "final$i"
done